【二】鸡汤来咯开发学习笔记-28

前端 · 2024-07-25

1.在untils中创建common.js(写工具方便调用,也就是公共js)

//封装弹窗
export function showToast(title="",icon="none",mask=true){
    let posttion =icon=='none'? 'bottom':'center';
    uni.showToast({
        title,
        icon,
        mask,
        position
    })
}

2.在edit中调用此方法在onsumit里面进行判断为空提示警告

const onSubmit =async() =>{
    if(formDate.value.content===""){
        return showToast("鸡汤内容不能为空")   //return在返回值后会直接必掉这整个函数
    }
    
    let res =await db.collection("soup-chicken").add(formDate.value)
    console.log(res);
}

3.在tool.js中加入工具清楚文本非法插入语句,然后在edit中引用使用

export function stripHTML(html) {  
  // 使用正则表达式替换掉HTML标签  
  var text = html.replace(/<[^>]*>?/gm, '');  
  return text;  
}  

使用

formDate.value.content =stripHTML(formDate.value.content);
Theme Jasmine by Kent Liao