作者: admin

  • vue中keepAlive使用

    keep-alive是vue内置的一个组件,而这个组件的作用就是能够缓存不活动的组件,我们能够知道,一般情况下,组件进行切换的时候,默认会进行销毁,如果有需求,某个组件切换后不进行销毁,而是保存之前的状态,那么就可以利用keep-alive来实现

    我这里利用脚手架创建项目后会生成home和about两个组件,并且通过路由进行切换

    //home组件
    <template>
     <div class="home">
     <input type="text">
    </div>
    </template>
    
    <script>
    // @ is an alias to /src
    import HelloWorld from '@/components/HelloWorld.vue'
    
    export default {
     name: 'home',
     components: {
       HelloWorld
     }
    }
    </script>

     

    //about组件
    <template>
     <div class="about">
     <input type="text">
    </div>
    </template>
    <script>
    export default {
     name:"about"
    }
    </script>

     

    当我们在home组件的输入框输入一些内容的时候,然后切换到about组件,在切换回home组件,我们会发现之前输入的内容被清空了,其实也容易理解,就是当切换到about组建的时候,home组件就被销毁了,输入框的值自然被清空了,如图:

    我在home组件写了destroyed生命周期函数

    那么此时我们就可以利用keep-alive组件进行包裹router-view组件,将不活动的组件缓存起来

    //App组件
    <template>
     <div id="app">
    
      <div id="nav">
        <router-link to="/">Home</router-link> |
        <router-link to="/about">About</router-link>
      </div>
      <keep-alive>
        <router-view />
      </keep-alive>
    
    </div>
    </template>

    写完之后会发现当切换到about组件时,home组件中的destroyed并没有触发,并且home组件的值也保存了下来,如图:

    home组件写了123,about组件写了456,来回切换数据都保存下来了。

    但是这样也肯定不好,我们会发现,about组件的值也被缓存了,就是所有的路由组件都被缓存了,严重浪费性能,而且也不符合需求,我们现在只想缓存home组件

     

    在keep-alive上有两个属性

    字符串或正则表达式。只有匹配的组件会被缓存。

    • include 值为字符串或者正则表达式匹配的组件name会被缓存。
    • exclude 值为字符串或正则表达式匹配的组件name不会被缓存。

    首先利用include实现,匹配到组件中定义的name,将进行缓存

    <keep-alive include="home">
     <router-view />
    </keep-alive>

    效果如图:只缓存了home的数据

    而exclude就是排除了,这个就不在演示了,很简单,除了这个我们还可以利用路由中的meta属性来控制

    {
     path: '/',
     name: 'home',
     meta:{
      keepAlive:true
     },
     component: Home
    }

    keep-alive代码可以结合v-if进行包裹,如果meta中的keepAlive为true进行缓存,否侧不进行缓存,这样可以更灵活一些

    <keep-alive>
     <router-view v-if="$route.meta.keepAlive" />
    </keep-alive>
    <router-view v-if="!$route.meta.keepAlive" />

    还有一个问题,home组件数据是缓存了,但是home组件的生命周期钩子也不会被调用,如果有业务逻辑涉及到回调的那就需要到keep-alive的生命周期钩子,所以此时要使用activated与deactivated来获取当前组件是否处于活动状态

    我在home组件里面写入了activated与deactivated生命周期函数

    activated(){
     console.log("哎呀看见我了")
     console.log("----------activated--------")
    },
    deactivated(){
     console.log("讨厌!!你又走了")
     console.log("----------deactivated--------")
    }

    https://blog.csdn.net/weixin_41819098/article/details/89379734

     

  • vue树节点创建以及增删改

    效果图:

     

    demo查看

     

    如果使用脚手架项目使用可能会报错

    [Vue warn]: You are using the runtime-only build of Vue where the template compiler is not available

     

     

    使用runtimeonly模式的,也就是引入.vue

    如果不是直接引入vue.js而是使用脚手架,你可能会启动

    module.exports = {
      runtimeCompiler: true
    }

    那就需要了解Compiler和Runtime的区别

    总之,Compiler模式支持编译template,慢;

    例如:

    new Vue({
     el: "#box",
     template: "<div>{{msg}}</div>",
     data: {
      msg: "hello"
     }
    });

    Runtime使用 vue-loader 加载.vue 文件,不支持直接用template,这种方式快。

    https://blog.csdn.net/xiaomajia029/article/details/88320233

     

     

    父:

    <template>
      <div class="app-container">
    
              <el-col :span="12">
                    <el-input v-model="firstlevel" style="width: 150px;"></el-input>
                    <el-button @click="addfirstlevel" type="primary">完成</el-button>
              </el-col>
              <el-col :span="12">
                    <treeMenus :toplevel="toplevel" :list="treeMenusData"></treeMenus>
                    <el-button @click="savetree" type="primary">完成</el-button>
              </el-col>
          
      </div>
    </template>
    
    <script>
    
    import $ from 'jquery'
    
    import TreeMenus from '@/components/TreeMenus'
    var tempid=0;
    export default {
      name: 'categoryManagement',
      components: {
        TreeMenus
      },
      data() {
        return {
          toplevel:1,
    
          firstlevel:'',
      
          treeMenusData: [ // 数据格式
            // {
            //   id:1,
            //   name:'一级1',
            //   children:[
            //     {
            //       id:8,
            //       name:'二级-1',
            //       children:[
            //         {
            //           id:3,
            //           name:'三级-1',
            //           children:[
            //             {
            //               id:11,
            //               name:'四级-1',
            //               children:[
            //
            //               ]
            //             },
            //           ]
            //         },
            //       ]
            //     },
            //     {
            //       id:77,
            //       name:'二级-2',
            //       children:[
            //
            //       ]
            //     }
            //   ]
            // },
            // {
            //   id:61,
            //   name:'一级2',
            //   children:[]
            // },
          ],
    
        }
      },
      watch: {
    
      },
      created() {
    
      },
      mounted() {
        
      },
      destroyed() {
        // window.removeEventListener('storage', this.afterQRScan)
      },
      methods: {
    
    
        findx:function(type,arr,id){
          for(let i=0;i<arr.length;i++){
    
            if( arr[i].id==id ){
              if(type=='del'){
                arr.splice(i,1)
              }else{
                arr[i].children.push({
                  id:this.findmaxValue(this.list)+1,
                  name:'',
                  children:[]
                })
              }
              break
            }else if(arr[i].children.length > 0){
              this.findx(type,arr[i].children, id);  //递归调用
            }
          }
        },
    
    
        findmaxValue(arr){
    
          for(let i=0;i<arr.length;i++){
            console.log(arr[i].id)
            if(arr[i].id>tempid){
              tempid=arr[i].id
            }
    
            if(arr[i].children.length > 0){
              this.findmaxValue(arr[i].children);  //递归调用
            }else{
             // break
            }
    
          }
          return tempid
        },
    
    
        //添加第一级
        addfirstlevel(){
          tempid =0
    
          this.treeMenusData.push({
            id:this.findmaxValue(this.treeMenusData)+1,
            name:this.firstlevel,
            children:[
            ]
          })
    
        },
    
     
    
        savetree(){
    
          console.log(this.treeMenusData)
    
        }
    
      }
    }
    </script>
    
    <style lang="scss">
      .el-pagination{display: inline-block}
      .treedom span{background: #ccc;cursor: pointer}
      li{list-style: none}
      em{cursor: pointer}
      .catename{padding: 10px 0;}
      .branch{display: none}
      #tree em{margin-right: 5px;}
      #tree li{margin: 15px 0}
      #tree .del{cursor: pointer;font-size: 12px; color: rgb(64, 158, 255);}
      #tree input{
        height: 36px;
        line-height: 36px;
        -webkit-appearance: none;
        background-color: #FFFFFF;
        background-image: none;
        border-radius: 4px;
        border: 1px solid #DCDFE6;
        -webkit-box-sizing: border-box;
        box-sizing: border-box;
        color: #606266;
        display: inline-block;
        font-size: inherit;
        outline: none;
        padding: 0 15px;
        -webkit-transition: border-color 0.2s cubic-bezier(0.645, 0.045, 0.355, 1);
        transition: border-color 0.2s cubic-bezier(0.645, 0.045, 0.355, 1);
      }
    
    </style>
    
    

     

    子:

    <template>
         <ul class="tree-ul">
           <li v-for="(item,index) in list " :key="index">
             <div> <span v-if="toplevel!=4" @click="addlevelbtn(item.id)"><i class="el-icon-circle-plus"></i></span> <el-input style="width: 150px" v-model="item.name"></el-input> <el-link @click="delfindid(item.id)" type="primary">删除</el-link> </div>
             <treeMenus :toplevel="toplevel+1" :list="item.children"></treeMenus>
           </li>
         </ul>
    
    </template>
    <script>
    
    var tempid=0
    export default {
      name: "treeMenus",
      props: {
        list: Array,
        toplevel:Number,
      },
      data(){
        return{
    
        }
      },
      computed:{
    
      },
      methods:{
        addlevelbtn(id){
          tempid=0
          this.findx('xx',this.list,id)
        },
    
    
        findmaxValue(arr){
    
          for(let i=0;i<arr.length;i++){
            console.log(arr[i].id)
            if(arr[i].id>tempid){
              tempid=arr[i].id
            }
    
            if(arr[i].children.length > 0){
              this.findmaxValue(arr[i].children);  //递归调用
            }else{
              // break
            }
    
          }
          return tempid
        },
    
        findx:function(type,arr,id){
          for(let i=0;i<arr.length;i++){
    
            if( arr[i].id==id ){
              if(type=='del'){
                arr.splice(i,1)
              }else{
                arr[i].children.push({
                  id:this.findmaxValue(this.list)+1,
                  name:'',
                  children:[]
                })
              }
              break
            }else if(arr[i].children.length > 0){
              this.findx(type,arr[i].children, id);  //递归调用
            }
          }
        },
    
        delfindid:function(id){
            this.findx('del',this.list,id)
        }
    
      }
    };
    
    </script>
    <style>
    .tree-ul li{margin: 10px 0;}
    </style>

    关于vue组件递归简单案例:https://www.cnblogs.com/gsgs/p/6687030.html

     

  • 解决:main redeclared in this block

    C:\Users\admin\Desktop\enter>go install ./...
    # _/C_/Users/admin/Desktop/enter
    .\move.go:5:6: main redeclared in this block
        previous declaration at .\enter.go:15:6

    每个目录只能有一个main函数,不然go install ./… 就会报错

     

     

     

     

  • package xxx is not in GOROOT

    我在导入gopath目录下的包时报错“package xxx is not in GOROOT“,编译器没有去gopath下找包,查了一下原因是GO111MODULE没有关, gomod 和 gopath 两个包管理方案,并且相互不兼容,在 gopath 查找包,按照 goroot 和多 gopath 目录下 src/xxx 依次查找。在 gomod 下查找包,解析 go.mod 文件查找包,mod 包名就是包的前缀,里面的目录就后续路径了。在 gomod 模式下,查找包就不会去 gopath 查找,只是 gomod 包缓存在 gopath/pkg/mod 里面。

     

    解决方法:

    把GO111MODULE置为off就行了。