1.先安装vue-cli
先打开cmd黑窗口(windows+输入cmd)
npm install -g vue-cli
npm install -g webpack
分别输入上面两条语句
安装成功后输入vue -V如果安装成功会显示对应版本号
并且会生成新的文件
2.构建项目
vue init webpack spa1
此命令用于创建SPA项目,它会在当前目录生成一个以“spa1”命名的文件夹
spa1即为项目名,项目名不能用中文或大写字母
注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//仅运行时,已经有推荐了就选择第一个了
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!
# =======================
步骤二:运行完上面的命令后,我们需要将当前路径改变到SPA这个文件夹内,然后安装需要的模块
此步骤可理解成:maven的web项目创建成功后,修改pom文件添加依赖
cd spa1 #改变路径到spa1文件夹下
npm install #安装所有项目需要的npm模块
步骤三:启动并访问项目
此步骤可理解成:启动tomcat,并通过浏览器访问项目
cd spa1 跳到项目目录
npm run dev 启动项目
注1:项目启动成功后,打开浏览器输入“http://localhost:8080”即可
项目就搭建完成了
在index.js中修改端口号,避免被占用
3、嵌套路由
新建vue
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>
Home.vue
<template>
<div>
博客内容
</div>
</template>
<script>
export default {
name: 'Home',
data () {
return {
}
}
}
</script>
About.vue
<template>
<div>
<router-link to="/Aboutme">站长</router-link>
<router-link to="/AboutWebSite">站点</router-link>
<router-view/>
</div>
</template>
<script>
export default {
name: 'About',
data () {
return {
}
}
}
</script>
AboutMe.vue
<template>
<div>
站长资料
</div>
</template>
<script>
export default {
name: 'Aboutme',
data () {
return {
}
}
}
</script>
AboutWebSize.vue
<template>
<div>
站点背景
</div>
</template>
<script>
export default {
name: 'AboutWebSite',
data () {
return {
}
}
}
</script>
src里面的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: 'HelloWorld',
// component: HelloWorld
// },
{
path: '/Home',
name: 'Home',
component: Home
},
{
path: '/',
name: 'Home',
component: Home
},
{
path: '/About',
name: 'About',
component: About,
children: [{
path: '/Aboutme',
name: 'Aboutme',
component: Aboutme
}
,
{
path: '/AboutWebSite',
name: 'AboutWebSite',
component: AboutWebSite
}
]
}
]
})