vue+layui实现select动态加载后台数据

刚开始由于layui form渲染与vue渲染有时间差 有时会导致 select里面是空白的
后来就想办法 等vue数据渲染完 再渲染layui form
试过模块化导入layui form组件 然后等vue数据渲染完后手动进行渲染
这种方式有一个小问题 有时候会提示render方法未定义
可能是由于执行顺序原因 vue先执行了

最后把vue代码放到layui.use里面 问题解决

可能不是最好的实现方式 如有更好的实现方式欢迎指出 共同进步

页面代码

<div id="demo" class="layui-inline layui-form" lay-filter="test2">
  <select>
   <option v-for="option in options" v-bind:value="option.id">
    {{ option.name }}
  </option> </select>
</div>

js

var vue = new Vue({
              el: '#demo',
              data: {
                option: {}, 
                options: []
              },
              created: function () {
                    this.send();
              },
              updated: function () {
                    layui.form.render('select','test2');
                    console.log(layui.form);
              },
              methods:{
                  send() {
                      axios({
                          method:'get',
                          url:'${contextPath}/find?page=1&limit=100'
                      }).then(resp => {
                          this.options = resp.data.data;
                          console.log(resp.data.data);                
                      }).catch(resp => {
                          console.log('请求失败:'+resp.status+','+resp.statusText);
                      });
                  }  
              },
            })

猜你喜欢

转载自blog.csdn.net/qq_26814945/article/details/82019346