直接引用vue.js怎样使用路由


当前第2页 返回上一页

1, 引入 vue.JS 和 vue-router.JS

1

2

3

4

<script src="https://unpkg.com/vue/dist/vue.js">

</script>

<script src="https://unpkg.com/vue-router/dist/vue-router.js">

</script>

2, 创建挂载的 dom 元素

1

<div id="app"></App>

3, 创建路由组件

1

2

3

4

5

6

const com1 = {

template: '<div > 路由 1</div>'

}

const com2 = {

template: '<div > 路由 2</div>'

}

4, 定义路由

1

2

3

4

const routes = [

   { path: '/hash1', component: com1 },

   { path: '/hash2', component: com2 }

]

5, 创建 router 实例

1

2

3

const router = new VueRouter({

   routes

})

6, 创建 vue 实例并挂载

1

2

3

const App = new Vue({

  router

}).$mount('#app');

下面是具体的代码:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

<!DOCTYPE HTML>

<HTML>

   

  <head>

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <meta http-equiv="X-UA-Compatible" content="ie=edge">

    <title>

      Document

    </title>

    <script src="https://unpkg.com/vue/dist/vue.js">

    </script>

    <script src="https://unpkg.com/vue-router/dist/vue-router.js">

    </script>

  </head>

   

  <body>

    <div id="app">

      <h1>

        Hello !

      </h1>

      <p>

        <!-- 使用 router-link 组件来导航. -->

        <!-- 通过传入 `to` 属性指定链接. -->

        <!-- <router-link> 默认会被渲染成一个 `<a>` 标签 -->

        <router-link to="/hash1">

          切换至 com1

        </router-link>

        <router-link to="/hash2">

          切换至 com2

        </router-link>

      </p>

      <!-- 路由出口 -->

      <!-- 路由匹配到的组件将渲染在这里 -->

      <router-view>

      </router-view>

      <!-- router-link 上的其他属性: -->

      <!-- 设置 replace 属性的话, 当点击时, 会调用 router.replace() 而不是 router.push(), 导航后不会留下

      history 记录. -->

      <!-- <router-link :to="{ path:'/abc'}" replace></router-link> -->

      <!-- 有时候想要 <router-link> 渲染成某种标签, 例如 <li>. 于是我们使用

      tag prop 类指定何种标签, 同样它还是会监听点击, 触发导航. -->

      <!-- <router-link to="/foo" tag="li">foo</router-link> -->

      <!-- active-class 设置 链接激活时使用的 CSS -->

      <!-- event 声明可以用来触发导航的事件. 可以是一个字符串或是一个包含字符串的数组. -->

    </div>

  </body>

  <script>

    // 1. 定义 (路由) 组件.

    const com1 = {

      template: '<div > 路由 1</div>'

    }

    const com2 = {

      template: '<div > 路由 2</div>'

    }

    // 2. 定义路由

    // 每个路由应该映射一个组件. 其中 "component" 可以是 通过 Vue.extend()

    //  创建的组件构造器, 或者, 只是一个组件配置对象.

    const routes = [{

      path: '/hash1',

      component: com1

    },

    {

      path: '/hash2',

      component: com2

    }]

    // 3. 创建 router 实例, 然后传 `routes` 配置

    const router = new VueRouter({

      routes // (缩写)相当于 routes: routes

    })

    // 4. 创建和挂载根实例.

    // 要通过 router 配置参数注入路由, 从而让整个应用都有路由功能

    const App = new Vue({

      router

    }).$mount('#app'); //el 是自动挂载, mount 是手动挂载(延时)

     

  </script>

  

</HTML>

相关学习推荐:js视频教程

以上就是直接引用vue.js怎样使用路由的详细内容,更多文章请关注木庄网络博客

返回前面的内容

相关阅读 >>

聊一聊angular中的路由(routing)

什么是前端路由及解释

浅析angular路由中的懒加载、守卫、动态参数

vue+webpack2实现路由懒加载的方法介绍

vue路由守卫有哪三种类型

10个关于路由vue-router的vuejs面试题(含答案解析)

详解angular中的route路由

如何使用nodejs实现路由功能

直接引用vue.js怎样使用路由

详解angular中的路由及其用法

更多相关阅读请进入《引用vue.js》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...