Todo List:
vue add @vue/typescript
tsconfig.json
<script lang="ts">
import Vue from 'vue'
export default Vue.extend({
data() {
return {
a: 1,
b: '2',
c: {},
d: []
}
},
methods:{
test() {
this.c() // error tips
}
}
})
</script>
vue-class-component
import Vue from 'vue'
import Component from 'vue-class-component'
import home from "../views/home.vue";//导入组件
@Component({
components: { home },
props: {
propMessage: String
}
})
export default class App extends Vue {
// 初始 data
msg = 123
// use prop values for initial data
helloMsg = 'Hello, ' + this.propMessage
// 生命钩子lifecycle hook
mounted () {
this.greet()
}
// 计算属性computed
get computedMsg () {
return 'computed ' + this.msg
}
// 方法method5
greet () {
alert('greeting: ' + this.msg)
}
}
export default Vue.extend({...})
,而不是export default {...}
'extends': [
'plugin:vue/essential',
'@vue/standard',
'@vue/typescript'
],
rules: {
'no-console': process.env.NODE_ENV === 'production' ? 'error' : 'off',
'no-debugger': process.env.NODE_ENV === 'production' ? 'error' : 'off'
},
"semi": ["error", "always"]
规则不生效可以尝试清楚校验结果缓存.cache
npm i element-ui -S
这个ui库是比较适合做中后台的管理系统的。
完整引入
import Vue from 'vue';
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import App from './App.vue';
Vue.use(ElementUI);
new Vue({
el: '#app',
render: h => h(App)
});
按需引入
需要借助插件,借助 babel-plugin-component
,我们可以只引入需要的组件,以达到减小项目体积的目的。
安装 babel-plugin-component:npm install babel-plugin-component -D
将 .babelrc 修改为:
{
"presets": [["es2015", { "modules": false }]],
"plugins": [
[
"component",
{
"libraryName": "element-ui",
"styleLibraryName": "theme-chalk"
}
]
]
}
如果你只希望引入部分组件,比如 Button 和 Select,那么需要在 main.js 中写入以下内容:
import Vue from 'vue';
import { Button, Select } from 'element-ui';
import App from './App.vue';
Vue.component(Button.name, Button);
Vue.component(Select.name, Select);
/* 或写为
* Vue.use(Button)
* Vue.use(Select)
*/
new Vue({
el: '#app',
render: h => h(App)
});
完整组件列表和引入方式(完整组件列表以 components.json 为准)
需要注意的是,样式文件需要单独引入。import 'element-ui/lib/theme-chalk/index.css';
全局配置
引入 Element 时,可以传入一个全局配置对象。该对象目前支持 size 与 zIndex 字段。size 用于改变组件的默认尺寸,zIndex 设置弹框的初始 z-index(默认值:2000)。按照引入 Element 的方式,具体操作如下:
import Vue from 'vue';
import Element from 'element-ui';
Vue.use(Element, { size: 'small', zIndex: 3000 });
import Vue from 'vue';
import { Button } from 'element-ui';
Vue.prototype.$ELEMENT = { size: 'small', zIndex: 3000 };
Vue.use(Button);
styles/variables.scss
<style lang="scss" scoped>
@import "~@/styles/variables.scss";
.text {
color: $success-color;
}
style>
每次用到的时候都要import,比较麻烦,重复的动作也容易出错。所以有一种更好的办法,把它注入到全局。 也就是在webpack配置与处理器Loader传递选项。可以使用vue.config.js
中的css.loaderOptions
选项,例如:可以这样向所有sass/less样式传入共享的全局变量。module.exports = {
css: {
loaderOptions: {
sass: {
prependData: `@import "~@/styles/variables.scss"`
},
scss: {
prependData: `@import "~@/styles/variables.scss"`
},
less: {
globalVars: {
primary: '#fff'
}
}
}
}
}
devServer.proxy
选项。devServer: {
proxy: {
'/boss': {
target: 'http://eduboss.lagounews.com',
// ws: true,
changeOrigin: true // 把请求头中的host转换成target值
}
}
}
axios
库: npm i axios
src/utils/request.ts
import axios from 'axios'
const Request = axios.create({
// 配置选项
// baseURL
// timeout
})
// 请求拦截器
// 响应拦截器
export default Request
/
⾸⻚/login
⽤户登录/role
⻆⾊管理/menu
菜单管理/resource
资源管理/course
课程管理/user
⽤户管理/advert
⼴告管理/advert-space
⼴告位管理// router.ts
import Vue from 'vue'
import Router from 'vue-router'
import Home from './views/Home.vue'
Vue.use(Router)
// 路由配置规则
export default new Router({
routes: [
{
name: 'home',
path: '/',
component: () => import(/* webpackChunkName: 'home' */'@/views/home/index.vue')
},
{
name: 'login',
path: '/login',
component: () => import(/* webpackChunkName: 'login' */'@/views/login/index.vue')
},
{
name: 'user',
path: '/user',
component: () => import(/* webpackChunkName: 'user' */'@/views/user/index.vue')
},
{
name: 'advert',
path: '/advert',
component: () => import(/* webpackChunkName: 'advert' */'@/views/advert/index.vue')
},
{
name: 'advert-space',
path: '/advert-space',
component: () => import(/* webpackChunkName: 'advert-space' */'@/views/advert-space/index.vue')
},
{
name: 'course',
path: '/course',
component: () => import(/* webpackChunkName: 'course' */'@/views/course/index.vue')
},
{
name: 'menu',
path: '/menu',
component: () => import(/* webpackChunkName: 'menu' */'@/views/menu/index.vue')
},
{
name: 'resource',
path: '/resource',
component: () => import(/* webpackChunkName: 'resource' */'@/views/resource/index.vue')
},
{
name: 'role',
path: '/role',
component: () => import(/* webpackChunkName: 'role' */'@/views/role/index.vue')
},
{
name: '404',
path: '*', // 配置not found fallback,建议配置到最后一个的位置,避免因为版本问题,可能*之后配置路由找不到
component: () => import(/* webpackChunkName: '404' */'@/views/error-page/404.vue')
}
]
})
// 添加了页面布局过后的路由配置
import Vue from 'vue'
import Router from 'vue-router'
Vue.use(Router)
// 路由配置规则
export default new Router({
routes: [
{
name: 'login',
path: '/login',
component: () => import(/* webpackChunkName: 'login' */'@/views/login/index.vue')
},
{
path: '/',
component: () => import(/* webpackChunkName: 'layout' */'@/layout/index.vue'),
children: [
{
name: 'home',
path: '', // 默认子路由
component: () => import(/* webpackChunkName: 'home' */'@/views/home/index.vue')
},
{
name: 'user',
path: '/user',
component: () => import(/* webpackChunkName: 'user' */'@/views/user/index.vue')
},
{
name: 'advert',
path: '/advert',
component: () => import(/* webpackChunkName: 'advert' */'@/views/advert/index.vue')
},
{
name: 'advert-space',
path: '/advert-space',
component: () => import(/* webpackChunkName: 'advert-space' */'@/views/advert-space/index.vue')
},
{
name: 'course',
path: '/course',
component: () => import(/* webpackChunkName: 'course' */'@/views/course/index.vue')
},
{
name: 'menu',
path: '/menu',
component: () => import(/* webpackChunkName: 'menu' */'@/views/menu/index.vue')
},
{
name: 'resource',
path: '/resource',
component: () => import(/* webpackChunkName: 'resource' */'@/views/resource/index.vue')
},
{
name: 'role',
path: '/role',
component: () => import(/* webpackChunkName: 'role' */'@/views/role/index.vue')
}
]
},
{
name: '404',
path: '*', // 配置not found fallback,建议配置到最后一个的位置,避免因为版本问题,可能*之后配置路由找不到
component: () => import(/* webpackChunkName: '404' */'@/views/error-page/404.vue')
}
]
})
model
rules
prop
application/json
application/x-www-form-urlencoded
multipart/form-data
router.beforeEach
), next必须被调用。如果路由比较多,有些需要有些不需要守卫,那么在配置路由的时候我们就可以给路由配置meta元信息,再在守卫函数中进行获取meta元素数据进行判断处理该如何处理,页面是否允许跳转。access_token
: 获取需要授权的接口数据expires_in
: access_token
的过期时间refresh_token
: 刷新获取新的access_token
access_token
过期时,可以使用refresh_token
去重新获取新的access_token
,调用刷新token接口。access_token
需要比较短的过期时间,出去安全考虑,降低账户信息泄露的风险。access_token
过期的处理
expires_in
字段支持,当是两个机器时间不一致的时候就会出现问题。