简体   繁体   English

如何在 Vue 中更改 url?

[英]How do I change url in the Vue?

How do we make a change url in the Vue?我们如何在 Vue 中更改 url?

If I logout now and login with a different ID, the page remains unchanged.如果我现在注销并使用不同的 ID 登录,则页面保持不变。 because the URL has not changed.因为 URL 没有改变。

the url format is base/UserRecommend/{user_id} url 格式为base/UserRecommend/{user_id}

I'm trying to switch pages using userid.我正在尝试使用用户 ID 切换页面。 The userid has been properly changed, but the url has not changed at all.用户 ID 已正确更改,但 url 根本没有更改。

App.vue应用程序.vue

<div id="nav">
    <router-link v-if="isLogin" :to="{ name:'UserRecommend',params:{userid:this.userid}}">UserRecommend</router-link>
</div>

<script>
import jwt_decode from "jwt-decode"
export default {
  name: 'App',
  data: function () {
    return {
      isLogin: false,
    }
  },
  methods: {
    logout () {
      this.isLogin = false
      localStorage.removeItem('jwt')
      localStorage.removeItem('username')
      this.$router.push({ name:'Home' })
    },
  },
  created: function () {
    if (localStorage.getItem('jwt')) {
      const token = localStorage.getItem('jwt')
      this.userid= jwt_decode(token).user_id
      this.isLogin = true
      this.username = localStorage.getItem('username')
      this.$router.push({ name:'Home' })
    }
  }
}
</script>

Here, take the parameter value with routerlink and move the page.在这里,用routerlink取参数值并移动页面。 However, if I press link UserRecommend , the url is expected to change, but it remains as the previous url and page但是,如果我按链接UserRecommend , url 预计会改变,但它仍然是以前的 url 和页面

If I refresh the page, the url is changed.如果我刷新页面,url 会更改。

UserRecommend.vue用户推荐.vue

<template>
  <div>
    <h1>"{{this.$route.params.userid}}"</h1>
    <ul class="row">
      <MovieItem v-for="movie in movieList" :key="movie.id" :movie="movie" @click="getMovieDetail(movie.id)"/>
    </ul>
  </div>
</template>

<script>
import jwt_decode from "jwt-decode"
import axios from 'axios'
import MovieItem from '@/components/MovieItem'
export default {
  name:'UserRecommend',
  data(){
    return {
      movieList:[],
    }
  },
  components:{
    MovieItem,
  },
  created(){
    const token = localStorage.getItem('jwt')
    this.userid= jwt_decode(token).user_id
    console.log(this.userid)
    console.log(token)
    axios({
      url:'http://127.0.0.1:8000/memovies/movies/'+this.userid+'/recommend/',
      method:'get',
      headers:{
          Authorization: `JWT ${localStorage.getItem('jwt')}`
      },
    })
      .then(res=>{
        console.log(this.userid)
        this.movieList = res.data
        console.log(res.data)
        console.log(this.userid)
      })
      .catch(err=>{
        console.log(this.$route.params.userid)
        console.log(err)
      })
  },
}
</script>

<style>

</style>

router路由器

  {
    path:'/memovies/UserRecommend/:userid',
    name:'UserRecommend',
    component: UserRecommend,
  },

How do I change the url when I press 'UserRecommend' on the main page?当我在主页上按“UserRecommend”时,如何更改 url?

Additionally, what is missing about my process in the vue?此外,我在 vue 中的流程缺少什么?

Ummm, I suggest that you should replace the following lines:嗯,我建议你应该替换以下几行:

    <h1>"{{this.$route.params.userid}}"</h1>

with:和:

    <h1>"{{$route.params.userid}}"</h1>

because... wait, it's another problem.因为...等等,这是另一个问题。 Let's focus on how to change the url.让我们关注如何更改url。 How about changing this:如何改变这个:

    <router-link v-if="isLogin" :to="{ name:'UserRecommend',params:{userid:this.userid}}">UserRecommend</router-link>

to this:对此:

    <router-link v-if="isLogin" :to="{ name:'UserRecommend',params:{userid}}">UserRecommend</router-link>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM