课程目标:
1、使用vue-cli搭建SPA项目
2、如何在SPA项目中使用路由、组件
3、如何使用嵌套路由
一、使用vue-cli搭建SPA项目
1、什么是vue-cli
vue-cli是vue.js的脚手架,用于自动生成vue.js+webpack的项目模板,创建命令如下: vue init webpack xxx
注1:xxx 为自己创建项目的名称
注2:必须先安装vue,vue-cli,webpack,node等一些必要的环境
2. 安装vue-cli
在命令提示符cmd中依次运行以下代码:
npm install -g vue-cli
npm install -g webpack
安装成功后,会出现如下文件:
3、创建SPA项目
在存放HBuilder X软件所存放的文件中执行命令提示符(cmd)操作
执行的命令:
vue init webpack spa1
#此命令用于创建SPA项目,它会在当前目录生成一个以“spa1”命名的文件夹
#spa1即为项目名,项目名不能用中文或大写字母,然后终端会出现“一问一答”模式(见注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//仅运行时,已经有推荐了就选择第一个了
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安装依赖全部选择好回车就进行了生成项目,出现如下内容表示项目创建完成
# Project initialization finished!
# ========================
实在不会选,就回车选择“默认”或是选择“N”不安装
成功:
4、将所创建的文件引入到HBuider X
文件——打开目录——选中所创建文件
在index.js文件中改端口号,为了区分与tomcat的端口号,避免端口号被占用
package.json详解:
每个项目的根目录下面,一般都有一个package.json文件,定义了这个项目所需要的各种模块,
以及项目的配置信息(比如名称、版本、许可证等元数据)。npm install命令根据这个配置文件,
自动下载所需的模块,也就是配置项目所需的运行和开发环境
详情见资料“package-详解.json”中的相关注释
npm install/npm install xxx -S/npm install xxx -D/npm install xxx -g的区别:
1 、npm install
下载“package.json”中dependencies和devdependencies中配置的所有依赖模块,并保存到项目的node_modules目录注1:在git clone项目的时候,项目文件中并没有node_modules文件夹,为什么呢?
2、 npm install xxx -g
全局安装,下载依赖模块,并保存到%node_home%\node_global\node_modules目录下3、 npm install xxx -S
写入到package.json的dependencies对象,并保存到项目的node_modules目录
4、 npm install xxx -D
写入到package.json的devDependencies对象,并保存到项目的node_modules目录
4、vue项目结构说明
build文件夹(了解) 这个文件夹主要是进行webpack的一些配置
webpack.base.conf.js webpack基础配置,开发环境,生产环境都依赖
webpack.dev.conf.js webpack开发环境配置
webpack.prod.conf.js webpack生产环境配置
build.js 生产环境构建脚本
vue-loader.conf.js 此文件是处理.vue文件的配置文件 config文件夹config文件夹
dev.env.js 配置开发环境
prod.env.js 配置生产环境
index.js 这个文件进行配置代理服务器,例如:端口号的修改
node_modules文件夹 存放npm install时根据package.json配置生成的npm安装包的文件夹src文件夹 源码目录(开发中用得最多的文件夹)
assets 共用的样式、图片
components 业务代码存放的地方,里面分成一个个组件存放,一个页面是一个组件,一个页面里面还会包着很多组件
router 设置路由
App.vue vue文件入口界面
main.js 对应App.vue创建vue根实例,也是入口文件,对应webpack.base.config.js里的入口配置static文件夹 存放的文件不会经过webpack处理,可以直接引用,例如swf文件如果要引用可以在webpack配置。对swf后缀名的文件处理的loader,也可以直接将swf文件放在这个文件夹引用
package.json 这个文件有两部分是有用的:scripts 里面设置命令以及在dependencies和devDependencies中,分别对应全局下载和局部下载的依赖包
二、嵌套路由
完成以下效果:
2、定义组件,定义页签,锚点,
组件:在components文件中新建vue文件
Home.vue:
<template>
<div>
显示博客内容
</div>
</template><script>
export default{
data(){
return{};
}
}
</script><style>
</style>
About.vue:
<template>
<div>
关于
</div>
</template><script>
export default{
data(){
return{};
}
}
</script><style>
</style>
页签,锚点:在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>
2、建立关系
在router文件中的index.js文件中建立关系
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
}
]
})
3、配置路由器
src文件中的main.js文件
// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'Vue.config.productionTip = false
/* eslint-disable no-new */
new Vue({
el: '#app',
router,
components: { App },
template: '<App/>'
})
4、嵌套
在关于的组件中再定义个几个页签,锚点
About.vue:
<template>
<div>
<router-link to="/AboutMe">关于博主</router-link>
<router-link to="/AboutWebSite">关于网站</router-link>
<router-view/>
</div>
</template><script>
export default{
data(){
return{};
}
}
</script><style>
</style>
再定义两个组件:
AboutMe:
<template>
<div>
显示博主相关个人事迹
</div>
</template><script>
export default{
data(){
return{};
}
}
</script><style>
</style>
AboutWebSite:
<template>
<div>
显示网站发展历史
</div>
</template><script>
export default{
data(){
return{};
}
}
</script><style>
</style>
再到router文件中的index.js文件中定义
import Vue from 'vue'
import Router from 'vue-router'
import HelloWorld from '@/components/HelloWorld'
import Home from '@/components/Home'
import About from '@/components/About'
import AboutMe from '@/components/AboutMe'
import AboutWebSite from '@/components/AboutWebSite'
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,
children:[
{
path: '/AboutMe',
name: 'AboutMe',
component: AboutMe
},
{
path: '/AboutWebSite',
name: 'AboutWebSite',
component: AboutWebSite
}
]
}
]
})
完成效果:
本期内容结束~