vue2.0路由设置
参考文章 :
Vue2.0 新手完全填坑攻略——从环境搭建到发布
http://www.jianshu.com/p/5ba253651c3b
使用路由器前必须先安装
安装 vue 路由模块vue-router和网络请求模块vue-resource
cnpm install vue-router vue-resource --save
启动项目
npm run dev
1.App.vue文件的更改
//添加视图层/渲染层
<router-view></router-view>
//使用 router-link 组件来导航,通过传入 `to` 属性指定链接,<router-link> 默认会被渲染成一个 `<a>` 标签
<router-link to="/Tt" :style="classObject">Tt</router-link>
2.main.js文件的更改
//引入路由并使用路由
import VueRouter from 'vue-router'
Vue.use(VueRouter);
//1. 定义(路由)组件
import Tt from './component/Title.vue'
import server from './component/server.vue'
//创建路由实例
const router = new VueRouter({
// 定义路径(path)
base: __dirname,
routes: [
// 每个路由应该映射一个组件。 其中"component" 可以是通过 Vue.extend() 创建的组件构造器,或者,只是一个组件配置对象。
{ path: '/Tt', component: Tt },
{ path: '/server', component: server
children:[
// 当 /user/:id 匹配成功,
// UserHome 会被渲染在 User 的 <router-view> 中
{ path: '', component: UserHome},
// 当 /user/:id/profile 匹配成功,
// UserProfile 会被渲染在 User 的 <router-view> 中
{ path:'profile', component:UserProfile },
// 当 /user/:id/posts 匹配成功
// UserPosts 会被渲染在 User 的 <router-view> 中
{ path: 'posts', component: UserPosts }
]
},
]
})
// 刷新页面时返回指定路由页面组件
router.push("server");
怎么安装配置路由
//创建和挂载根实例
// 生命周期改为 mount函数 不需要进行挂载
// const app = new Vue({ router:router }).$mount('#app');