1、安装vue-cli
打开cmd命令
npm install -g vue-cli
npm install -g webpack
然后进行安装,安装成功后你的node-v10.15.3-win-x64目录下的node_global会多出
vue
vue.cmd
vue-init
vue-init.cmd
vue-list
vue-list.cmd的文件
安装完成之后打开命令窗口并输入 vue -V(注意这里是大写的“V”),如果出现相应的版本号,则说明安装成功。
然后再输入vue init webpack spa1 spa1即为项目名,项目名不能用中文或大写字母,然后终端会出现“一问一答”模式
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安装依赖
vue项目结构说明:
config文件夹
dev.env.js 配置开发环境
prod.env.js 配置生产环境
index.js 这个文件进行配置代理服务器,例如:端口号的修改
整个项目只有一个html页面
代码:
这是需要用到的几个文件
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 aboutwbsite from '@/components/aboutwbsite' 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: '/aboutwbsite', name: 'aboutwbsite', component: aboutwbsite } ] } ] })
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>
将组件放到index.js进行配置就可以了
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="/aboutwbsite">关于</router-link> <router-view/> </div> </template> <script> export default{ data(){ return{ }; } } </script> <style> </style>
结果: