[前端] html中使用vue-router的示例代码

1679 0
王子 2022-10-19 16:02:06 | 显示全部楼层 |阅读模式
引入vue和vue-router
  1. <script src="https://unpkg.com/vue/dist/vue.js"></script>
  2. <script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
复制代码
完整的示例
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6.     <meta http-equiv="X-UA-Compatible" content="ie=edge">
  7.     <title>Document</title>
  8.     <script src="https://unpkg.com/vue/dist/vue.js"></script>
  9.     <script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
  10. </head>
  11. <body>
  12.     <div id="app">
  13.         <h1>Hello !</h1>
  14.         <p>
  15.           <!-- 使用 router-link 组件来导航. -->
  16.           <!-- 通过传入 `to` 属性指定链接. -->
  17.           <!-- <router-link> 默认会被渲染成一个 `<a>` 标签 -->
  18.           <router-link to="/hash1">切换至com1</router-link>
  19.           <router-link to="/hash2">切换至com2</router-link>
  20.         </p>
  21.         <!-- 路由出口 -->
  22.         <!-- 路由匹配到的组件将渲染在这里 -->
  23.         <router-view></router-view>
  24.         <!-- router-link上的其他属性: -->
  25.         <!-- 设置 replace 属性的话,当点击时,会调用 router.replace() 而不是 router.push(),
  26.             导航后不会留下 history 记录。 -->
  27.         <!-- <router-link :to="{ path: '/abc'}" replace></router-link> -->
  28.         <!-- 有时候想要 <router-link> 渲染成某种标签,例如 <li>。 于是我们使用
  29.          tag prop 类指定何种标签,同样它还是会监听点击,触发导航。 -->
  30.         <!-- <router-link to="/foo" tag="li">foo</router-link> -->
  31.         <!-- active-class 设置 链接激活时使用的 CSS  -->
  32.         <!-- event 声明可以用来触发导航的事件。可以是一个字符串或是一个包含字符串的数组。 -->
  33.     </div>
  34. </body>
  35. <script>
  36.     // 1. 定义(路由)组件。
  37.     const com1 = { template: '<div>路由1</div>' }
  38.     const com2 = { template: '<div>路由2</div>' }
  39.     // 2. 定义路由
  40.     // 每个路由应该映射一个组件。 其中"component" 可以是 通过 Vue.extend()
  41.     //  创建的组件构造器, 或者,只是一个组件配置对象.
  42.     const routes = [
  43.         { path: '/hash1', component: com1 },
  44.         { path: '/hash2', component: com2 }
  45.     ]
  46.     // 3. 创建 router 实例,然后传 `routes` 配置
  47.     const router = new VueRouter({
  48.         routes // (缩写)相当于 routes: routes
  49.     })
  50.     // 4. 创建和挂载根实例。
  51.     // 要通过 router 配置参数注入路由,从而让整个应用都有路由功能
  52.     const app = new Vue({
  53.         router
  54.     }).$mount('#app');//el是自动挂载,mount是手动挂载(延时)
  55. </script>
  56. </html>
复制代码
到此这篇关于html中使用vue-router的示例代码的文章就介绍到这了,更多相关html使用vue-router内容请搜索中国红客联盟以前的文章或继续浏览下面的相关文章,希望大家以后多多支持中国红客联盟!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

中国红客联盟公众号

联系站长QQ:5520533

admin@chnhonker.com
Copyright © 2001-2025 Discuz Team. Powered by Discuz! X3.5 ( 粤ICP备13060014号 )|天天打卡 本站已运行