Home > Web Front-end > Vue.js > How to implement page jump in vuejs

How to implement page jump in vuejs

青灯夜游
Release: 2021-09-28 14:44:12
Original
14907 people have browsed it

Vuejs method to implement page jump: 1. Use the "..." statement; 2. Use "this .$router.push({path: 'page url address' })" statement.

How to implement page jump in vuejs

The operating environment of this tutorial: windows7 system, vue2.9.6 version, DELL G3 computer.

Two ways for Vue routing to implement page jumps (router-link and JS)

1. Through Implementation

The component is used to set a navigation link and switch different HTML content

Usage:

  • Simple writing method

<router-link to="demo2">demo2</router-link>
Copy after login
  • ##Using v-bind writing method

<router-link :to="&#39;demo2&#39;">demo2</router-link>

<!-- 也可以用{}包裹对应的path或name -->
<router-link :to="{ name: &#39;demo2&#39; }">demo2</router-link>
Copy after login
  • How to write parameters

<router-link :to="{ name: &#39;demo2&#39;, params: { userId: 123 }}">demo2</router-link>
Copy after login
Passing parameters here needs to be done in

router.js Configure the path of demo2 in , and add the wildcard after demo2 in the path: and the corresponding userId, as follows:

{
  path: '/demo2/:userId',
  name: 'demo2',
  component: demo2
  },
Copy after login
After the configuration is completed, the page The result of the jump is

/demo2/123

The "123" here is the above

userId

So, how to add it to the new page? How about getting the passed parameter

userId?

Use

this.$route.params.xx in the mounted hook. Get the passed parameters, as follows:

mounted () {
    alert(this.$route.params.userId)
}
// 弹出123
Copy after login
  • Incoming address key-value pair

<router-link :to="{ path: &#39;demo2&#39;, query: { plan: &#39;private&#39; }}">demo2</router-link>
Copy after login
The page jump result is

/demo2?plan=private

(Note that there is no need to configure the path in

router.js)

To obtain the passed address key-value pair plan in the new page, you can

mounted Use this.$route.plan.xx in the hook. Get the passed address key-value pair, as follows:

mounted () {
  alert(this.$route.query.plan)
}

// 弹出private
Copy after login
2, Implemented through JS ---this.$router.push()


template part:

<button @click="toURL">跳转页面</button>
Copy after login
script part:

(Note Here is router, above is route)

  • Simple writing method

methods:{
  toURL(){
      this.$router.push({ path: '/demo2' })
  }
}
Copy after login
  • Passing parameters Writing method

methods:{
  toURL(){
      this.$router.push({ name: 'demo2', params: { userId: 123 }})
  }
}
Copy after login
  • Incoming address key-value pair

methods:{
  toURL(){ 
     this.$router.push({ name: 'demo2', params: { userId: 123 }, query: { plan: 'private' } })
  }
}
Copy after login
Related recommendations :《

vue.js tutorial

The above is the detailed content of How to implement page jump in vuejs. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template