课程目标
一、 如何使用vue-cli搭建SPA项目
二、 如何在spa项目中使用路由
三、嵌套路由的使用
一、 如何使用vue-cli搭建SPA项目
- 构建前提是nodeJS环境已经搭建完毕。
- 安装vue-cli
2.1 什么是vue-cli: vue-cli是vue.js的脚手架,用于自动生成vue.js+webpack的项目模板。
2.2 开始安装:执行下面命令语句
npm install -g vue-cli
npm install webpack -g
执行结果:
安装好后会在node_global文件中多出来文件
测试:
- 使用脚手架vue-cli(2.X版)来构建项目
3.1 步骤一:使用脚手架创建项目骨架
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.1 Runtime + Compiler: recommended for most users//运行加编译,官方推荐,就选它了
4.2 Runtime-only: about 6KB lighter min+gzip, but templates (or any Vue-specific HTML) are ONLY allowed in .vue files
- render functions are required elsewhere//仅运行时,已经有推荐了就选择第一个了
5. Install 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安装依赖全部选择好回车就进行了生成项目,出现如下内容表示项目创建完成
3.2 步骤二:运行完上面的命令后,我们需要将当前路径改变到SPA这个文件夹内,然后安装需要的模块
cd spa1: 改变路径到spa1文件夹下
npm install: 安装所有项目需要的npm模块
3.3 步骤三:启动并访问项目
npm run dev
注1:项目启动成功后,打开浏览器输入“http://localhost:8080”即可
注2:vue-cli构建的项目,在控制台npm run dev启动后,默认的调试地址是8080端口的但是大部分时候,
我们都要并行几个项目开发,很有可能已经占用了8080端口,所以就涉及到如何去更改调试地址的端口号了
config --> index.js
dev: {
// Paths
assetsSubDirectory: 'static',
assetsPublicPath: '/',
proxyTable: {},
host: 'localhost',
port: 8083, // 在这里修改端口号
autoOpenBrowser: false,
errorOverlay: true,
notifyOnErrors: true,
},
- vue项目结构说明
config文件夹
dev.env.js 配置开发环境
prod.env.js 配置生产环境
index.js 这个文件进行配置代理服务器,例如:端口号的修改
src文件夹 源码目录(开发中用得最多的文件夹)
assets 共用的样式、图片
components 业务代码存放的地方,里面分成一个个组件存放,一个页面是一个组件,一个页面里面还会包着很多组件
router 设置路由
App.vue vue文件入口界面
main.js 对应App.vue创建vue实例,也是入口文件,对应webpack.base.config.js里的入口配置
package.json 这个文件有两部分是有用的:scripts 里面设置命令以及在dependencies和devDependencies中,分别对应全局下载和局部下载的依赖包
二、 如何在spa项目中使用路由
- 将原有的路由设置取消
- 自定义一个Home.vue文件
- 修改src/router/index.js路由配置
三、嵌套路由的使用
Home.vue
<template>
<div>
放首页的内容
</div>
</template>
<script>
export default {
data() {
return {
};
}
}
</script>
<style>
</style>
UserManage.vue
<template>
<div>
<router-link to="/UserManage/UserDetail">用户详情</router-link>
<router-link to="/UserManage/UserPwd">更换密码</router-link>
<router-view/>
</div>
</template>
<script>
export default {
data() {
return {
};
}
}
</script>
<style>
</style>
UserDetail.vue
<template>
<div>
用户详情界面
</div>
</template>
<script>
export default {
data() {
return {
};
}
}
</script>
<style>
</style>
UserPwd.vue
<template>
<div>
用户密码管理界面
</div>
</template>
<script>
export default {
data() {
return {
};
}
}
</script>
<style>
</style>
index.js
import Vue from 'vue'
import Router from 'vue-router'
import HelloWorld from '@/components/HelloWorld'
import Home from '@/components/Home'
import UserManage from '@/components/UserManage'
import UserDetail from '@/components/UserDetail'
import UserPwd from '@/components/UserPwd'
Vue.use(Router)
export default new Router({
routes: [
{
path: '/',
name: 'Home',
component: Home
},
{
path: '/Home',
name: 'Home',
component: Home
},
{
path: '/UserManage',
name: 'UserManage',
component: UserManage,
children:[
{
path: '/UserManage/UserDetail',
name: 'UserDetail',
component: UserDetail,
},
{
path: '/UserManage/UserPwd',
name: 'UserPwd',
component: UserPwd,
}
]
}
]
})
app.vue
<template>
<div id="app">
<!-- <img src="./assets/logo.png"> -->
<router-link to="/Home">首页</router-link>
<router-link to="/UserManage">用户信息管理</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>
结果展示: