先下载Node.js,上面网址进行安装
- 在cmd中运行node -v命令,查看是否能够输出版本号
- 在cmd中运行npm -v命令,查看是否能够输出版本号
* 安装node.js淘宝镜像加速器(cnpm)
- # -g 就是全局安装
- npm install cnpm -g
-
- # 或使用如下语句解决npm速度慢的问题,但是每次install都需要
- npm install --registry=https://registry.npm.taobao.org
- cnpm instal1 vue-cli-g
- #测试是否安装成功#查看可以基于哪些模板创建vue应用程序,通常我们选择webpack
- vue list

创建第一个Vue程序,需要先找好一个目录,进入那个目录下使用Windows命令:
vue init webpack myvue
全都选no

之后项目创建成功,再进行配置项目环境
然后 ,进入创建好的项目,输入cd myvue:
输入:npm install
当出现问题时,它会给出提示我们按照提示来就行

访问localhost:8080即可

进入项目, 输入上述命令:npm install vue-router --save-dev
如果运行错误,更换cnpm install vue-router --save-dev
在启动Vue项目如果启动不了,运行错误,更换以下运行版本 npm i vue-router@3.1.3
- import Vue from 'vue'
- import VueRouter from 'vue-router'
-
- Vue.use(VueRouter);
测试路由:

定义几个自己的组件 Content.vue 、Main.vue、Kuang.vue
COntent.vue:
- <template>
- <h1>内容页</h1>
- </template>
-
- <script>
- export default {
- name: "Content.vue"
- }
- </script>
-
- <style scoped>
-
- </style>
Main.vue:
- <template>
- <h1>首页</h1>
- </template>
-
- <script>
- export default {
- name: "Main"
- }
- </script>
-
- <style scoped>
-
- </style>
Kuang.vue:
- <template>
- <h1>Kuangshen</h1>
- </template>
-
- <script>
- export default {
- name: "Kuang"
- }
- </script>
-
- <style scoped>
-
- </style>
router,专门存放路由,配置路由index.js
- import Vue from 'vue'
- import VueRouter from 'vue-router'
- import Content from '../components/Content'
- import Kuang from '../components/Kuang'
-
- import Main from '../components/Main'
- //安装路由
- Vue.use(VueRouter);
-
- //配置导出路由
- export default new VueRouter({
- routes:[
- {
- //路由路径
- path:'/content',
- name:'content',
- //跳转的组件
- component:Content
- },
- {
- //路由路径
- path:'/main',
- name:'content',
- //跳转的组件
- component:Main
- },
- {
- //路由路径
- path:'/kuang',
- name:'content',
- //跳转的组件
- component:Kuang
- }
- ]
- });
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/>'
- })
App.vue中使用路由- <template>
- <div id="app">
- <img src="./assets/logo.png">
- <h1>Vue-Router</h1>
- <router-link to="/main">首页</router-link>
- <router-link to="/content">内容页</router-link>
- <router-link to="/kuang">Kuang</router-link>
- <router-view></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.html
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width,initial-scale=1.0"> <title>myvue</title> </head> <body> <div id="app"></div> <!-- built files will be auto injected --> </body> </html>
运行npm run dev,然后浏览器访问localhost:8080

点击首页:
点击内容页:
点击kaung: