什么是vue-cli?
vue-cli是vue.js的脚手架,用于自动生成vue.js+webpack的项目模板
安装vue-cli
打开cmd ,执行以下代码:
npm install -g vue-cli
npm install -g webpack
安装完成后,node.js里会出现如下文件:
注1:安装完成之后打开命令窗口并输入 vue -V(注意这里是大写的“V”),如果出现相应的版本号,则说明安装成功
使用脚手架vue-cli(2.X版)来构建项目
1、先创建一个空的项目(文件夹),之后打开cmd
也可以使用以下方法:
如果在文件找中不到命令窗口 ,可以看看这篇博客:https://blog.csdn.net/licmi/article/details/108059521
2、创建项目
vue init webpack 项目名
之后会进入“一问一答”:
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 runnpm 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”不安装
注1:cmd命令行窗口显示中文乱码,多是因为cmd命令行窗口字符编码不匹配导致
修改cmd窗口字符编码为UTF-8,命令行中执行:chcp 65001
切换回中文:chcp 936
这两条命令只在当前窗口生效,重启后恢复之前的编码。
3、启动并运行项目
将项目导入HBuilderX里面:
找到自己项目所在路径进行选择即可
之后找到config文件夹下面打开index.js修改端口为8083
注1:vue-cli构建的项目,在控制台npm run dev启动后,默认的调试地址是8080端口的但是大部分时候,
我们都要并行几个项目开发,很有可能已经占用了8080端口,所以就涉及到如何去更改调试地址的端口号了
打开App.vue文件直接运行等待cmd窗口加载好后就直接复制地址在浏览器地址栏访问即可
SPA嵌套路由案例
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>
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 AboutWebSite from '@/components/AboutWebSite'
import AboutMe from '@/components/AboutMe'
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
}
]
}
]
})
后面都是components文件中的
Home.vue
<template>
<div>
显示博客内容
</div>
</template>
<script>
export default{
data(){
return{
};
}
}
</script>
<style>
</style>
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.vue
<template>
<div>
显示博主相关个人事迹
</div>
</template>
<script>
export default{
data(){
return{
};
}
}
</script>
<style>
</style>
AboutWebSite.vue
<template>
<div>
显示网站发发展历史
</div>
</template>
<script>
export default{
data(){
return{
};
}
}
</script>
<style>
</style>
最终的效果图: