路由模式

Hash: 使用URL的hash值来作为路由。支持所有浏览器 这种为默认模式 URL中带有#的

History: 当你使用 history 模式时,URL 就像正常链接,例如 http://yoursite.com/user/id,也好看!

Abstract: 支持所有javascript运行模式。如果发现没有浏览器的API,路由会自动强制进入这个模式

linkActiveClass

linkActiveClass 该配置为激活状态时动态添加的class名称

页面结构

<body>
    <div id="app">
        <h1>路由守卫</h1>

        <!-- 路由渲染组件 -->
        <router-view></router-view>

        <br />

        <router-link to="/">跳转首页</router-link>
        <router-link to="/user">跳转会员中心</router-link>
        <router-link to="/product">跳转产品页</router-link>
    </div>
</body>

<script src="./vue.min.js"></script>
<!--要引入路由组件-->
<script src="./vue-router.js"></script>

逻辑结构

const Home = {
    template: `<h1>首页</h1>`
}

const User = {
    template: `<h1>会员中心</h1>`
}

const Product = {
    template: `<h1>产品中心</h1>`
}

//创建路由对象
const router = new VueRouter({
    mode: 'hash', //默认模式
    linkActiveClass:'active', //路由跳转时动态添加样式
    routes: [
        {
            path: '/',
            component: Home,
        },
        {
            path: '/user',
            component: User,
        },
        {
            path: '/product',
            component: Product,
        },
    ]
})

//在全局中加载路由
var app = new Vue({
    el:'#app',
    router,
})
powered by GitbookEdit Time: 2023-04-08 10:28:32