vue路由


Vue.js 路由允许我们通过不同的 URL 访问不同的内容。

通过 Vue.js 可以实现多视图的单页Web应用(single page web application,SPA)。

Vue.js 路由需要载入 vue-router 库

1、引入js

<script src="vue.min.js">script>
<script src="vue-router.min.js">script>

2、编写html

<div id="app">
    <h1>Hello App!h1>
    <p>
        
        
        
        <router-link to="/">首页router-link>
        <router-link to="/student">会员管理router-link>
        <router-link to="/teacher">讲师管理router-link>
    p>
    
    
    <router-view>router-view>
div>

3、编写js

<script>

  // 1. 定义(路由)组件。
 
  // 可以从其他文件 import 进来

  const Welcome = { template: '
欢迎
' } const Student = { template: '
student list
' } const Teacher = { template: '
teacher list
' } // 2. 定义路由 // 每个路由应该映射一个组件。 const routes = [ { path: '/', redirect: '/welcome' }, //设置默认指向的路径 { path: '/welcome', component: Welcome }, { path: '/student', component: Student }, { path: '/teacher', component: Teacher } ] // 3. 创建 router 实例,然后传 `routes` 配置 const router = new VueRouter({ routes // (缩写)相当于 routes: routes }) // 4. 创建和挂载根实例。 // 从而让整个应用都有路由功能 const app = new Vue({ el: '#app', router }) // 现在,应用已经启动了! script>