VueRouter的使用

第一步router文件夹里新建index.js

// router文件夹里面的index.js

// 1. 引入vue和vue-router
import Vue from 'vue'
import VueRouter from 'vue-router'

// 引入需要的组件
import Home from '../components/Home.vue'
import About from '../components/About.vue'

// 2. 在vue中使用路由
Vue.use(VueRouter)

// 从路由对象中抽出来写,看起来简介,里面一个{}对应一个URL
// path:是路径的意思,这里跟url地址一样
// component:是注册组件,进入那个path就直接注册这个组件
// redirect:重定向的意思,刚打开页面的时候就让他重定向到那个界面
const abcd = [
  {
    path: '',
    redirect: '/home'
  },
  {
    path: '/home',
    component: Home
  },
  {
    path: '/about',
    component: About
  }
]

// 3. 创建路由对象
const router = new VueRouter({
  // routers是固定写法
  routes: abcd,
  // 默认是HASH模式,这里可以用mode改成history模式
  mode: 'history'
})

// 4. 导出路由
export default router

第二步,导出路由之后在main.js文件中引入并且使用

import Vue from 'vue'
import App from './App.vue'
// 引入刚刚导出的router文件
import router from './router'

Vue.config.productionTip = false

new Vue({
  // 在vue里使用它
  router,
  render: h => h(App),
}).$mount('#app')

第三步,在components文件夹里面新建两个Vue组件

// Home.vue
<template>
  <div>
    <p>我是Home组件</p>
  </div>
</template>

<script>
  export default {
    name: 'Home' 
  }
</script>

<style scoped>

</style>
// About.vue
<template>
  <div>
    <p>我是About组件</p>
  </div>
</template>

<script>
  export default {
    name: 'About' 
  }
</script>

<style scoped>

</style>

第四步,在App.vue组件里就可以开始使用了
router-link标签里面有: to 、replace、 append、 tag、 active-class、 exact 、 event、 exact-active-class这些属性;这里不再一一解释,可以去官网查看

// 方法1
<template>
  <div id="app">
  	 // 路由跳转,到页面上就是个a标签,里面的to属性就是跳转到那个url的
    <router-link to="/home">首页</router-link>
    <router-link to="/about">关于</router-link>
    // 渲染路由跳转的页面
    <router-view></router-view>
  </div>
</template>

<script>
export default {
  name: 'App'
}
</script>

<style>

</style>

// 方法2
<template>
  <div id="app">
    <button @click="homeBtn">首页</button>
    <button @click="aboutBtn">关于</button>
    <router-view></router-view>
  </div>
</template>

<script>
export default {
  name: 'App',
  methods: {
    homeBtn() {
      // 在 Vue 实例内部,你可以通过 $router 访问路由实例
      this.$router.push('/home')
    },
    aboutBtn() {
      this.$router.push('/about')
    }
  }
}
</script>

<style>

</style>

Vue RouterVue.js官方提供的路由管理器。通过创建一个router对象,我们可以实现路由跳转和路由的重定向。在使用Vue Router时,我们可以通过<router-link>和<router-view>标签来进行路由的使用和展示。其中,<router-view>是一个组件的名称,当url匹配到对应的路径时,对应的组件会被渲染到<router-view>标签中。 在Vue Router的基本配置中,我们可以创建路由组件和配置路由。首先,我们需要创建一个路由组件,比如一个名为Page的组件。然后,我们可以创建一个routes数组,其中包含了我们的路由配置,比如{ path: "/page", component: Page }。最后,我们可以创建一个router对象,将routes配置传入其中。 在Vue Cli中使用Vue Router时,有多种配置方式可供选择。你可以自己手动配置路由,也可以直接使用Vue Cli提供的脚手架工具来安装并配置Vue Router。无论使用哪种方式,你都可以将router对象放在Vue实例中,并通过this.$router来获取router对象进行操作。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [VueRouter使用](https://blog.csdn.net/m0_56232007/article/details/118412371)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [vue-router使用](https://blog.csdn.net/weixin_41223530/article/details/120672295)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值