一、Vue CLI目录概览
二、vue cli中的组件
< template>
< div>
< div class = " container-fluid" >
< div class = " col-sm-5 col-sm-offset-4" >
< h2> {{msg}}</ h2>
</ div>
< div class = " col-sm-6 col-sm-offset-3" >
< form class = " form-horizontal" >
< div class = " form-group" >
< label class = " col-sm-2 control-label" > 用户名</ label>
< div class = " col-sm-10" >
< input type = " text" v-model = " admin.username" class = " form-control" >
</ div>
</ div>
< div class = " form-group" >
< label class = " col-sm-2 control-label" > 密码</ label>
< div class = " col-sm-10" >
< input type = " password" v-model = " admin.password" class = " form-control" >
</ div>
</ div>
</ form>
</ div>
< div class = " col-sm-5 col-sm-offset-4" >
< input type = " button" style = " " value = " 登录" class = " btn btn-block btn-primary" @click = " login" >
</ div>
</ div>
</ div>
</ template>
< script>
import instance from "../utils/request" ;
export default {
name : "Login" ,
data ( ) {
return {
msg : '欢迎登录' ,
admin : { } ,
}
} ,
methods : {
login ( ) {
console. log ( "登录数据" , this . admin) ;
instance. post ( '/login' , this . admin) . then ( res => {
const result = res. data;
if ( result. success) {
localStorage. setItem ( "token" , result. token) ;
this . $router. push ( { name : 'Index' } ) ;
} else {
alert ( result. msg) ;
}
} ) ;
}
}
}
</ script>
< style scoped >
</ style>
< template>
< ! -- 主页组件-- >
< div>
< ! -- 展示公共组件-- >
< Header> < / Header>
< ! -- 展示路由 注意:router- view标签需要一个根标签进行包含! -- >
< router- view> < / router- view>
< / div>
< / template>
< script>
import instance from "../utils/request" ;
import Header from "../components/Header" ;
export default {
name : "Index" ,
components : { Header} ,
}
< / script>
< style scoped>
< / style>
< template>
< div>
< h2> { { msg} } , < a href= "#/" > 返回主页< / a> < / h2>
< / div>
< / template>
< script>
export default {
name : "404" ,
data ( ) {
return {
msg : '当前页面走丢了'
}
}
}
< / script>
< style scoped>
h2{
color : red;
}
< / style>
< template>
< div>
< h3> 用户列表< / h3>
< / div>
< / template>
< script>
export default {
name : "Index"
}
< / script>
< style scoped>
< / style>
三、vue cli中的路由
import Vue from 'vue'
import Router from 'vue-router'
Vue. use ( Router)
const originalPush = Router . prototype. push;
Router . prototype. push = function ( location ) {
return originalPush . call ( this , location) . catch ( err => err)
} ;
export default new Router ( {
routes : [
{
path : '/login' ,
name : 'Login' ,
component : ( ) => import ( '../views/Login' )
} ,
{
path : '/index' ,
name : 'Index' ,
component : ( ) => import ( '../views/Index' ) ,
children : [
{
path : '/emps' ,
name : 'Emps' ,
component : ( ) => import ( '../views/emp/Index' )
} ,
{
path : '/depts' ,
name : 'Depts' ,
component : ( ) => import ( '../views/dept/Index' )
} ,
{
path : '/users' ,
name : 'Users' ,
component : ( ) => import ( '../views/user/Index' )
} ,
{
path : '/orders' ,
name : 'Orders' ,
component : ( ) => import ( '../views/order/Index' )
} ,
] ,
} ,
{
path : '/' ,
redirect : '/index'
} ,
{
path : '*' ,
name : '404' ,
component : ( ) => import ( '../views/404' )
} ,
]
} )
四、vue cli中的公共组件
< template>
< div>
< div class = "container-fluid" >
< div class = "row" >
< div class = "col-sm-12" >
< h2> 欢迎进入员工管理系统< span v- show= "isLogin" > ,欢迎{ { admin. username} } < / span> < / h2>
< / div>
< / div>
< div class = "row" >
< div style= "float: right" >
< a href= "" v- show= "isLogin" @click. prevent= "logOut" > 退出登录< / a>
< router- link : to= "{name: 'Login'}" v- show= "!isLogin" > 立即登录< / router- link>
< / div>
< / div>
< / div>
< router- link : to= "{name: 'Emps'}" > 员工管理< / router- link> & nbsp; & nbsp;
< router- link : to= "{name: 'Depts'}" > 部门管理< / router- link> & nbsp; & nbsp;
< router- link : to= "{name: 'Users'}" > 用户管理< / router- link> & nbsp; & nbsp;
< router- link : to= "{name: 'Orders'}" > 订单管理< / router- link>
< / div>
< / template>
< script>
import instance from "../utils/request" ;
export default {
name : "Header" ,
data ( ) {
return {
msg : '主页组件' ,
admin : { }
}
} ,
created ( ) {
let token = localStorage. getItem ( "token" ) ;
instance. get ( '/token?token=' + token) . then ( res => {
this . admin = res. data;
} )
} ,
computed : {
isLogin ( ) {
return this . admin. username;
}
} ,
methods : {
logOut ( ) {
let token = localStorage. getItem ( "token" ) ;
instance. delete ( '/token?token=' + token) . then ( res => {
this . admin = { } ;
} )
}
}
}
< / script>
< style scoped>
< / style>
五、vue cli中的工具包
import axios from "axios" ;
const instance = axios. create ( {
timeout : 5000 ,
baseURL : 'http://localhost:8989'
} ) ;
instance. interceptors. request. use ( config => {
return config;
} ) ;
instance. interceptors. response. use ( response => {
return response;
} , error => {
alert ( '当前服务不可用,请稍后重试!' )
} ) ;
export default instance;
六、vue cli中的根组件 App.vue
< template>
< div id= "app" >
< ! -- 展示路由组件-- >
< router- view/ >
< / div>
< / template>
< script>
export default {
name : 'App'
}
< / script>
< style>
#app {
font- family: 'Avenir' , Helvetica, Arial, sans- serif;
- webkit- font- smoothing: antialiased;
- moz- osx- font- smoothing: grayscale;
text- align: center;
color : #2c3e50;
}
a{
font- size: 18px;
}
< / style>
七、vue cli中的主入口 main.js
import Vue from 'vue'
import App from './App'
import router from './router'
import '@/assets/bootstrap/css/bootstrap.css'
Vue. config. productionTip = false
new Vue ( {
el : '#app' ,
router,
components : { App } ,
template : '<App/>'
} )