和vue2对比
具体案例
新建 mixins文件夹
import * as directives from "./directives"
import wxjButton from "./components/wxjButton/index.vue"
let show = ( ) => console. log ( "show" )
export default {
install ( app ) {
app. config. globalProperties= show
Object. keys ( directives) . forEach ( k => app. directive ( k, directives[ k] ) )
app. component ( "wxjButton" , wxjButton)
console. log ( app) ;
}
}
组件内使用全局变量
import { getCurrentInstance } from "vue" ;
const currentInstance = getCurrentInstance ( ) ;
const message = currentInstance?. appContext. config. globalProperties. $message;
main.js中导入
import myPlugins from "./mixins"
app. use ( myPlugins) . use ( element) . use ( store) . use ( router) . mount ( '#app' )
directives.js
const color = {
inserted : function ( el, binding, vnode ) {
el. style. color = binding. value
} ,
}
let permission = {
inserted ( el, binding ) {
let value = binding. value || 'normal' ;
if ( value) {
let hasPermission = checkArray ( value) ;
if ( ! hasPermission) {
el. parentNode && el. parentNode. removeChild ( el) ;
}
}
}
}
function checkArray ( key = "normal" ) {
let arr = [ "normal" , "teacher" , "admin" ]
if ( arr. includes ( key) && key !== "normal" ) {
return true
} else {
return false
}
}
let permissionstore = {
inserted ( el, binding, vnode ) {
if ( vnode. context. $store. getters. isHas) {
el. parentNode && el. parentNode. removeChild ( el) ;
}
}
}
export {
permissionstore,
permission,
color
}
wxjButton.vue
< template>
< button @click= "wxj_btn_click" class = "wxj_button" >
< slot> Submit< / slot>
< / button>
< / template>
< script>
export default {
name: "wxjButton" ,
methods: {
wxj_btn_click ( ) {
this . $emit ( "click" )
}
} ,
} ;
< / script>
< style lang= 'less' scoped>
. wxj_button{
padding: 5 px 10 px;
font- size: 16 px;
outline: none;
border- radius: 3 px;
background- color: turquoise;
color: #fff;
border: none;
}
< / style>