1. 程式人生 > 其它 >2019狂神說VUE【8、404和路由鉤子】

2019狂神說VUE【8、404和路由鉤子】

<template>
  <div>
    <el-form ref="loginForm" :model="form" :rules="rules" label-width="80px" class="login-box">
      <h3 class="login-title">歡迎登入</h3>
      <el-form-item label="賬號" prop="username">
        <el-input type="text" placeholder="請輸入賬號" v-model="form.username"/>
      </el-form-item>
      <el-form-item label="密碼" prop="password">
        <el-input type="password" placeholder="請輸入密碼" v-model="form.password"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" v-on:click="onSubmit('loginForm')">登入</el-button>
      </el-form-item>
    </el-form>

    <el-dialog
      title
="溫馨提示" :visible.sync="dialogVisible" width="30%" :before-close="handleClose"> <span>請輸入賬號和密碼</span> <span slot="footer" class="dialog-footer"> <el-button type="primary" @click="dialogVisible = false">確 定</el-button> </span> </el-dialog> </div> </template> <script> export
default { name: "Login", data(){ return{ form: { username: '', password: '' }, //表單校驗 relus: { username: [ {required: true, message: '賬戶不可為空', trigger: 'blur'} ], password: [ {required:
true, message: '密碼不可為空', trigger: 'blur'} ], }, //對話方塊顯示和隱藏 dialogVisible: false } }, methods: { onSubmit(formName){ //為表單繫結驗證功能 this.$refs[formName].validate((valid) => { if (valid) { //使用vue-router路由到指定頁面,該方式稱之為程式設計式導航 // this.$router.push("/main"); this.$router.push("/main/" + this.form.username); } else { this.dialogVisible = true; return false; } }); } } } </script> <style scoped></style>
View Code
import Vue from 'vue'
import VueRouter from 'vue-router'

import Main from '../views/Main'
import Login from '../views/Login'
import UserList from '../views/user/List'
import UserProfile from '../views/user/Profile'

Vue.use(VueRouter);

export default new VueRouter({
  routes: [
    {
      path: '/login',
      component: Login
    },
    {
      // path: '/main',
      path: '/main/:name',
      props: true,
      component: Main,
      children: [ //巢狀路由
        {path: '/user/list', component: UserList},
        // {path: '/user/profile/:id', name: 'MyUserProfile', component: UserProfile} //接收引數-方式1
        {path: '/user/profile/:id', name: 'MyUserProfile', component: UserProfile, props: true} //接收引數-方式2
      ]
    },
    {
      path: '/goHome',
      redirect: '/main' //重定向
    }
  ]
});
View Code
<template>
    <div>
      <el-container>

        <el-aside width="200px">
          <el-menu :default-openeds="['1']">
            <el-submenu index="1">
              <template slot="title"><i class="el-icon-caret-right"></i>使用者管理</template>
              <el-menu-item-group>
                <el-menu-item index="1-1">
                  <!--<router-link to="/user/profile">個人資訊</router-link>-->
                  <router-link v-bind:to="{name:'MyUserProfile',params:{id:7}}">個人資訊</router-link>
                </el-menu-item>
                <el-menu-item index="1-2">
                  <router-link to="/user/list">使用者列表</router-link>
                </el-menu-item>
                <el-menu-item index="1-3">
                  <router-link to="/goHome">回到首頁</router-link>
                </el-menu-item>
              </el-menu-item-group>
            </el-submenu>
            <el-submenu index="2">
              <template slot="title"><i class="el-icon-caret-right"></i>內容管理</template>
              <e1-menu-item-group>
                <el-menu-item index="2-1">分類管理</el-menu-item>
                <el-menu-item index="2-2">內容列表</el-menu-item>
              </e1-menu-item-group>
            </el-submenu>
            <el-submenu index="3">
              <template slot="title"><i class="el-icon-caret-right"></i>系統管理</template>
              <e1-menu-item-group>
                <el-menu-item index="3-1">使用者設定</el-menu-item>
              </e1-menu-item-group>
            </el-submenu>
          </el-menu>
        </el-aside>

        <el-container>
          <el-header style="text-align: right; font-size: 12px">
            <el-dropdown>
              <i class="el-icon-setting" style="margin-right:15px"></i>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item>個人資訊</el-dropdown-item>
                <el-dropdown-item>退出登入</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
            <span>{{name}}</span>
          </el-header>

          <el-main>
            <router-view />
          </el-main>
        </el-container>

      </el-container>
    </div>
</template>

<script>
    export default {
        name: "Main",
        props: ['name']
    }
</script>


<style scoped lang="scss">
  .el-header {
    .el-header {
      backdrop-color: #269ad1;
      color: #333;
      line-height: 60px;
    }

    .el-aside {
      color: #269ad1;
    }
  }
</style>
View Code