Vue搭建SPA项目,使用路由及嵌套路由
一.Vue搭建SPA项目
需要提前搭建NodeJs环境)
安装Vue-li
npm install -g vue-cli
npm install -g webpack
利用Vue-cli来构建SPA项目
Vue-cli是Vue.js的脚手架,可以自动生成Vue.js+wbeback的项目模板,创建命令
步骤一:使用脚手架创建项目骨架
此步骤可理解成:使用eclipse创建一个maven的web项目
cmd #打开命令窗口
d: #切换到d盘
cd d:\temp #进入d:\temp目录
vue init webpack spa1 #此命令用于创建SPA项目,它会在当前目录生成一个以“spa1”命名的文件夹
#spa1即为项目名,项目名不能用中文或大写字母,然后终端会出现“一问一答”模式(见注2)
注1:cmd命令行窗口显示中文乱码,多是因为cmd命令行窗口字符编码不匹配导致
修改cmd窗口字符编码为UTF-8,命令行中执行:chcp 65001
切换回中文:chcp 936
这两条命令只在当前窗口生效,重启后恢复之前的编码。
注2:“一问一答”模式
1.Project name:项目名,默认是输入时的那个名称spa1,直接回车
2.Project description:项目描述,直接回车
3.Author:作者,随便填或直接回车
4.Vue build:选择题,一般选第一个
4.1Runtime + Compiler: recommended for most users//运行加编译,官方推荐,就选它了
4.2Runtime-only: about 6KB lighter min+gzip, but templates (or any Vue-specific HTML) are ONLY allowed in .vue files
render functions are required elsewhere//仅运行时,已经有推荐了就选择第一个了
vue-router:是否需要vue-router,Y选择使用,这样生成好的项目就会有相关的路由配置文件
6.Use ESLint to lint your code:是否用ESLint来限制你的代码错误和风格。N 新手就不用了,但实际项目中一般都会使用,这样多人开发也能达到一致的语法
7.Set up unit tests:是否安装单元测试 N
8.Setup e2e tests with Nightwatch?:是否安装e2e测试 N
9.Should we run npm install
for you after the project has been created? (recommended) (Use arrow keys)
> Yes, use NPM
Yes, use Yarn
No, I will handle that myself //选择题:选第一项“Yes, use NPM”是否使用npm install安装依赖
全部选择好回车就进行了生成项目,出现如下内容表示项目创建完成
在安装目录运行cmd,输入指令 npm run dev
项目启动成功后,打开浏览器输入“http://localhost:8080”即可
启动页面;
二在SPA项目中使用路由
创建.vue文件
导出vue文件
绑定路由关系
实例绑定和引入js已经由在项目创建时已经完成
在App.vue中发起请求显示内容,即可完成路由的创建及使用
代码:
Home.vue
<template>
<div>这是首页,展示网站最新的内容</div>
</template>
<script>
// 导出Vue文件
export default {
name: 'Home',
data () {
return {
msg: 'Welcome to Your Vue.js App'
}
}
}
</script>
<style>
</style>
About.vue
<template>
<div>关于,没什么好说的</div>
</template>
<script>
export default {
name: 'About',
data () {
return {
msg: 'Welcome to Your Vue.js App'
}
}
}
</script>
<style>
</style>
index.html
import Vue from 'vue'
import Router from 'vue-router'
//绑定路由关系
import HelloWorld from '@/components/HelloWorld'
import Home from '@/components/Home'
import About from '@/components/About'
Vue.use(Router)
export default new Router({
routes: [
{
path: '/',
name: 'Home',
component: Home
},
{
path: '/home',
name: 'Home',
component: Home
},
{
path: '/about',
name: 'About',
component: About
}
]
})
app.vue
<template>
<div id="app">
<!-- <img src="./assets/logo.png"> -->
<router-link to="/home">首页</router-link>
<router-link to="/about">关于</router-link>
<router-view/>
</div>
</template>
<script>
export default {
name: 'App'
}
</script>
<style>
#app {
font-family: 'Avenir', Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
</style>
运行结果:
三.嵌套路由的使用
在vue文件方面的操作与上面基本相同,创建vue,导出vue文件.
绑定路由关系这步时需要注意,仍然在index.html上部引入vue文件,如
import AboutWe from '@/components/AboutWe'
import AboutMe from '@/components/AboutMe'
但是下一步需要在父路由的children属性中进行:如
{
path: '/about',
name: 'About',
component: About,
children:[
{
path: '/aboutme',
name: 'AboutMe',
component: AboutMe
},
{
path: '/aboutwe',
name: 'AboutWe',
component: AboutWe
}
]
}
在父Vue文件的页面中发起请求显示内容,即可完成嵌套路由的使用
<template>
<!-- <div>关于,没什么好说的</div> -->
<div>
<router-link to="/aboutme">关于站长</router-link>
<router-link to="/aboutwe">关于网站</router-link>
<router-view></router-view>
</div>
</template>
<script>
export default {
name: 'About',
data () {
return {
msg: 'Welcome to Your Vue.js App'
}
}
}
</script>
<style>
</style>
``
运行结果:
/router-link>
<router-view></router-view>
</div>
</template>
<script>
export default {
name: 'About',
data () {
return {
msg: 'Welcome to Your Vue.js App'
}
}
}
</script>
<style>
</style>
运行结果: