vue vuerouter重定向

新建vue项目

 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">
            <button>搜索一下</button>
        </div>
        <div class="hot-link">
            热门搜索:
            <router-link to="/search/程序员">程序员</router-link>
            <router-link to="/search/前端培训">前端培训</router-link>
            <router-link to="/search/如何成为前端大牛">如何成为前端大牛</router-link>
        </div>
    </div>
</template>

<script>
    export default {
        name: "MyHome"
    }
</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.params.keywords }}</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.params.keywords);
        }
    }
</script>

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

index.js

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

Vue.use(VueRouter);

const router = new VueRouter({
   routes:[
        //重定向
      { path:'/',redirect:'/home'},
      { path:'/home',component:MyHome},
      { path:'/search/:keywords?',component: MySearch}
   ]
});

export  default router

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

虾米大王

有你的支持,我会更有动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值