vue vuerouter命名查询传参
  px8Y25gMnWbQ 2023年11月02日 53 0


新建vue项目

vue vuerouter命名查询传参_javascript

 main.js

import Vue from 'vue'
import App from './App.vue'
import router from "@/router";

Vue.config.productionTip = false;

new Vue({
  render: h => h(App),
  router
}).$mount('#app');

app.vue

<template>
  <div id="app">
    <div class="link">
      <router-link to="/home">首页</router-link>
      <router-link to="/search">搜索</router-link>
    </div>

    <router-view></router-view>
  </div>
</template>

<script>

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

<style>
  .link{
    height: 50px;
    line-height: 50px;
    background-color: #495150;
    display: flex;
    margin: -3px -3px 50px -3px;
  }

  .link a {
    width: 110px;
    height: 50px;
    line-height: 50px;
    background-color: red;
    display: inline-block;
    border-radius: 10px;
    margin-right: 5px;
    color: white;
    text-align: center;
    text-decoration: none;
  }

  *{
    margin: 0;
    padding: 0;
  }
</style>

MyHome.vue

<template>
    <div class="my-home">
        <div class="logo-box"><h1>黑马程序员</h1></div>
        <div class="search-box">
            <input type="text" v-model="inpValue">
            <button @click="goSearch">搜索一下</button>
        </div>
        <div class="hot-link">
            热门搜索:
            <router-link to="/search?key=程序员">程序员</router-link>
            <router-link to="/search?key=前端培训">前端培训</router-link>
            <router-link to="/search?key=如何成为前端大牛">如何成为前端大牛</router-link>
        </div>
    </div>
</template>

<script>
    export default {
        name: "MyHome",
        data(){
          return {
              inpValue:''
          }
        },
        methods:{
            goSearch(){
                //路径跳转
                //this.$router.push('/search');

                //路径跳转,完整写法
                // this.$router.push({
                //    path:'/search'
                // });

                //通过命名的方式条状,适合长路径,深路径时
                // this.$router.push({
                //     name:'search'
                // })

                //查询传参
                //this.$router.push(`/search?key=${this.inpValue}`);

                //
                // this.$router.push({
                //    path:'/search',
                //    query:{
                //        key:this.inpValue
                //    }
                // });

                //动态路由传参
                // this.$router.push(`/search/${this.inpValue}`);

                //动态路由传参,完整写法
                // this.$router.push({
                //     path:`/search/${this.inpValue}`,
                // });

                //命名,查询传参
                this.$router.push({
                    name:'search',
                    query:{
                        key:this.inpValue
                    }
                });

            }
        }
    }
</script>

<style scoped>
    .my-home{
        width: 50%;
        height: 500px;
        text-align: center;
    }
    .logo-box{
        margin-bottom: 40px;
    }
    .search-box input{
        width: 500px;
        height: 30px;
        line-height: 30px;
    }

    .search-box button{
        width: 170px;
        height: 35px;
        line-height: 35px;
        background-color: red;
        color:white;
    }

    .hot-link{
        margin-top: 18px;
    }
    .hot-link a{
        margin-right: 20px;
    }
</style>

MySearch.vue

<template>
    <div class="my-search">
        <p>搜索关键字:{{ $route.query.key }}</p>
        <p>搜索结果:</p>
        <ul>
            <li>..............</li>
            <li>..............</li>
            <li>..............</li>
            <li>..............</li>
        </ul>
    </div>
</template>

<script>
    export default {
        name: "MySearch",
        created() {
            //在created中,获取路由参数,需要加this
            console.log(this.$route.query.key);
        }
    }
</script>

<style scoped>
    .my-search{
        width: 400px;
        height: 240px;
        padding: 0 20px;
        border: 2px solid lightsalmon;
        margin-left: 100px;
        border-radius: 10px;
    }
</style>

NotFound.vue

<template>
    <div>
        <h1>404 Page Not Found!</h1>
    </div>
</template>

<script>
    export default {
        name: "NotFound"
    }
</script>

<style scoped>

</style>

index.js

import Vue from 'vue'
import VueRouter from "vue-router";
import MyHome from "@/views/MyHome";
import MySearch from "@/views/MySearch";
import NotFound from "@/views/NotFound";

Vue.use(VueRouter);

const router = new VueRouter({
   routes:[
      { path:'/',redirect:'/home'},
      { path:'/home',component:MyHome},
      { path:'/search',component: MySearch,name:'search'},
      { path: '*',component:NotFound}
   ]
});

export  default router
【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

推荐阅读
  gYl4rku9YpWY   2024年05月17日   57   0   0 Vue
  f18CFixvrKz8   2024年05月20日   88   0   0 JavaScript
  fxrR9b8fJ5Wh   2024年05月17日   52   0   0 JavaScript
  onf2Mh1AWJAW   2024年05月17日   59   0   0 Vue
  3A8RnFxQCDqM   2024年05月17日   58   0   0 Vue
px8Y25gMnWbQ