一:字符串去空格
let str = " ab ab ab "
str. trim ( ) ;
str. replaceAll ( " " , "" ) ;
str. replace ( / \s* / g , "" ) ;
str. replace ( / \ + / g , "" ) ;
二:vue中directives的用法 参考文档
directives 做权限按钮的功能
path : '/permission' ,
component : Layout,
name : '权限测试' ,
meta : { btnPermissions : [ 'admin' , 'supper' , 'normal' ] } ,
children : [
{
path : 'supper' ,
component : _import ( 'system/supper' ) ,
name : '权限测试页' ,
meta : { btnPermissions : [ 'admin' , 'supper' ] }
} ,
{
path : 'normal' ,
component : _import ( 'system/normal' ) ,
name : '权限测试页' ,
meta : { btnPermissions : [ 'admin' ] }
}
import Vue from 'vue'
const has = Vue. directive ( 'has' , {
bind : function ( el, binding, vnode ) {
let btnPermissions = vnode. context. $route. meta. btnPermissions. split ( "," ) ;
if ( ! Vue . prototype. $_has ( btnPermissions) ) {
el. parentNode. removeChild ( el) ;
}
}
} ) ;
Vue . prototype. $_has = function ( value ) {
let isExist = false ;
let btnPermissionsStr = sessionStorage. getItem ( "btnPermissions" ) ;
if ( btnPermissionsStr == undefined || btnPermissionsStr == null ) {
return false ;
}
if ( value. indexOf ( btnPermissionsStr) > - 1 ) {
isExist = true ;
}
return isExist;
} ;
export { has}
import has from './public/js/btnPermissions.js' ;
< el- button @click= 'editClick' type= "primary" v- has> 编辑< / el- button>
vue实现点击按钮输入框聚焦 v-focusAA=“focusState”, 修改input边框颜色 v-local-test
< template>
< div class = "hello" >
< ! -- 这里只能使用v- show, 不能使用v- if -- >
< input
type= "text"
@blur= "focusState = false"
v- focusAA= "focusState"
v- local- test
v- model= "testInput"
style= "width: 100%"
data- name= "自定义绑定数据"
/ >
< div @click= "focusClick" > 按钮< / div>
< / div>
< / template>
< script>
export default {
name : "ceShi" ,
data ( ) {
return {
testInput : '某某某' ,
focusState : false ,
} ;
} ,
methods : {
focusClick ( ) {
this . focusState = true ;
} ,
} ,
directives : {
focusAA : {
update : ( el, value ) => {
if ( value) {
el. focus ( ) ;
}
} ,
} ,
"local-test" : ( el, binding, vnode ) => {
el. style. border = "1px solid red" ;
console. log ( el. value) ;
console. log ( el. dataset. name) ;
console. log ( vnode. context. $route) ;
} ,
} ,
} ;
< / script>
三:部分手机图片变形,动态计算设置高度
< template>
< div class = "hello" >
< img : src= "imgSrc" : height= "imgHeight" / >
< / div>
< / template>
< script>
export default {
name : "ceShi" ,
data ( ) {
return {
imgSrc : '' ,
imgHeight : 0
} ;
} ,
created ( ) {
this . init ( )
} ,
methods : {
init ( ) {
_this = this ;
this . imgSrc = "https://img-home.csdnimg.cn/images/20201124032511.png"
let img = new Image ( ) ;
img. src = this . imgSrc
img. onload = function ( ) {
_this. imgHeight = ( window. innerWidth / img. width * img. height) + 'px'
}
} ,
} ,
} ;
< / script>