今天的搭建是在nodeJS环境搭建好后的基础上实行搭建的
什么是vue-cli
vue-cli是vue.js的脚手架,用于自动生成vue.js+webpack的项目模板,创建命令如下:
vue init webpack xxx
搭建步骤
一、安装vue-cli
执行代码:npm install -g vue-cli
成功后会在你的vue-global中新增一些文件
二、使用脚手架vue-cli来构建项目
1、使用脚手架创建项目骨架: vue init webpack xxx
(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安装依赖
选择完就会进入下载,下载完后进入你的项目
3、启动项目,运行npm run dev成功看到进入地址就是成功了,然后根据地址访问你的项目
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,
},
4、停止项目添加element-ui模块
npm install element-ui -S #-S就是-save的缩写
有概率失败,文件丢失,成功了就如下图
element-ui也加载好了后就可以运行一下我们今天的demo了
SPA项目的目录介绍
APP.vue是主页面所挂载的边界
main.js:挂载组件,每写一个组件都要在这挂载
components:放一些公用的html,比如导航条这些
views:放你所写的一些组件
assets:放项目所需的静态资料,图片等等
static:放静态css和js
router里面的index.js把你的路由都放进容器里面
config:放项目要运行的配置文件
介绍完了,就上今天的Demo了
index.js
import Vue from 'vue'
import Router from 'vue-router'
import HelloWorld from '@/components/HelloWorld'
import About from '@/views/About'
import UserInfo from '@/views/UserInfo'
import UserDetail from '@/views/UserDetail'
import UserPwd from '@/views/UserPwd'
Vue.use(Router)
export default new Router({
routes: [
{
path: '/',
name: 'About',
component: About
},
{
path: '/About',
name: 'About',
component: About
},
{
path: '/UserInfo',
name: 'UserInfo',
component: UserInfo,
children:[
{
path: '/UserDetail',
name: 'UserDetail',
component: UserDetail,
},
{
path: '/UserPwd',
name: 'UserPwd',
component: UserPwd,
}
]
}
]
})
About.vue
<template>
<div class="hello">
博主个人事迹
</div>
</template>
<script>
export default {
data() {
return {
};
}
}
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
h1, h2 {
font-weight: normal;
}
ul {
list-style-type: none;
padding: 0;
}
li {
display: inline-block;
margin: 0 10px;
}
a {
color: #42b983;
}
</style>
UserDetail
<template>
<div class="hello">
用户详情
</div>
</template>
<script>
export default {
data() {
return {
};
}
}
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
h1, h2 {
font-weight: normal;
}
ul {
list-style-type: none;
padding: 0;
}
li {
display: inline-block;
margin: 0 10px;
}
a {
color: #42b983;
}
</style>
UserInfo
<template>
<div class="hello">
<!-- <img src="./assets/logo.png"> -->
<router-link to="/UserDetail">用户详情</router-link>
<router-link to="/UserPwd">修改密码</router-link>
<router-view />
</div>
</template>
<script>
export default {
data() {
return {
};
}
}
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
h1,
h2 {
font-weight: normal;
}
ul {
list-style-type: none;
padding: 0;
}
li {
display: inline-block;
margin: 0 10px;
}
a {
color: #42b983;
}
</style>
UserPwd
<template>
<div class="hello">
用户密码修改
</div>
</template>
<script>
export default {
data() {
return {
};
}
}
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
h1, h2 {
font-weight: normal;
}
ul {
list-style-type: none;
padding: 0;
}
li {
display: inline-block;
margin: 0 10px;
}
a {
color: #42b983;
}
</style>
APP.vue
<template>
<div id="app">
<!-- <img src="./assets/logo.png"> -->
<router-link to="/About">about me</router-link>
<router-link to="/UserInfo">用户信息</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>