0


vue2路由跳转页面

前端工作笔记之---页面路由跳转vue2

前言

1.安装vue-router

  1. npm i vue-router

2.配置路由

在src文件夹下创建router文件夹,然后在router文件夹下创建index.js文件,如下图

在index.js文件夹中进行配置

  1. import Vue from 'vue'
  2. import VueRouter from 'vue-router'
  3. // 导入一级路由
  4. import Goods from '@/views/Goods'
  5. import Particulars from '@/views/Particulars'
  6. Vue.use(VueRouter)
  7. const routes = [{
  8. path: '/',
  9. redirect: '/goods'
  10. },
  11. {
  12. path: '/goods',
  13. name: 'goods',
  14. component: Goods,
  15. },
  16. {
  17. path: '/particulars',
  18. name: 'particulars',
  19. component: Particulars
  20. }
  21. ]
  22. const router = new VueRouter({
  23. routes
  24. })
  25. export default router

在src文件夹下创建view文件夹,在里边放路由文件,如下图

在main.js文件中注册路由

  1. import router from '@/router/index.js'
  1. new Vue({
  2. render: h => h(App),
  3. router
  4. }).$mount('#app')

接下来就是使用啦

一、单页面路由跳转

首先强调一点,使用路由跳转需要放置路由挂载点**<router-view></router-view>**,放置在你需要显示的地方。

这里我将挂载点放置在App.vue中了

  1. <template>
  2. <div id="app">
  3. <router-view></router-view>
  4. </div>
  5. </template>

1.使用<router-link></router-link>的方式进行跳转

  1. <router-link :to="{ path: '/particulars'}">跳转</router-link>

2.使用**

  1. this.$router.push()的方式进行跳转

**

  1. <el-button type="text" size="small" @click="toEdit()">编辑</el-button>
  1. methods: {
  2. toEdit() {
  3. this.$router.push({ path: '/particulars' })
  4. },
  5. },

**

  1. this.$router.replace(),用法同上。

**

二、跳转到新页面(打开一个新窗口)

1.<router-link></router-link>

  1. <router-link target="_blank" :to="{ path: '/catalog', query: { id: '1' } }"
  2. >打开新的标签页</router-link>

2.**

  1. this.$router.push()

**

  1. const routeUrl = this.$router.resolve({
  2. path: "/targetUrl",
  3. query: { id: 96 },
  4. });
  5. window.open(routeUrl.href, "_blank");
  6. },
标签: html5 css3 vue.js

本文转载自: https://blog.csdn.net/2201_75514915/article/details/129547169
版权归原作者 馨雨lxy 所有, 如有侵权,请联系我们删除。

“vue2路由跳转页面”的评论:

还没有评论