【问题标题】:Using component area to show various content使用组件区展示各种内容
【发布时间】:2018-05-25 04:39:41
【问题描述】:

我有一个相对简单的任务,虽然我只是一个初学者,所以很难继续。 我在左侧和右侧面板上有一个用户列表,以显示用户信息。有关用户的信息有一个编辑按钮,我想接管该右侧面板,然后保存将返回到用户详细信息。 解决此问题的最佳方法是什么? 这两个页面应该是不同的组件还是应该只使用 javascript 来显示和隐藏内容?有没有比这两种方法更好的方法? 抱歉,我是新手,只是想了解一下这个概念。

谢谢

【问题讨论】:

  • 这取决于你的页面内容的复杂程度和你的UI设计,建议你在两个组件之间切换,这样更适应
  • 我还建议使用 2 个不同的组件。更简洁的代码,你可以让它更容易由 url 驱动。 .../userId?edit 之类的内容将直接将您带到编辑组件,.../userId 将带您查看组件。

标签: vue.js components electron


【解决方案1】:

我为你写了一个简单的例子:

const data = [{
  id: 1,
  name: 'user1',
  age: 21
},{
  id: 2,
  name: 'user2',
  age: 33
}]

const mixin = {
  props: {
    userId: {
      required: true
    }
  },
  data () {
    return {
      user: {}
    }
  },
  methods: {
    loadUser () {
      /*ajax to get user detail data here*/
      setTimeout(_=>{
        this.user = data.filter(o=>o.id==this.userId)[0]
      },10)
    }    
  },
  created () {
    this.loadUser()
  },
  watch: {
    userId (newVal) {
      if(newVal){
        this.loadUser()
      }
    }
  }
}

Vue.component('user-viewer',{
  template: `<div>
    name:{{user.name}}<br>
    age: {{user.age}}<br>
    <button @click="edit">edit</button>
  </div>`,
  mixins: [mixin],
  methods: {
    edit () {
      this.$emit('switch-edit-mode',true)
    }
  }
});

Vue.component('user-editor',{
  template: `<div>
    name:<input type="text" v-model="user.name"><br>
    age: <input type="text" v-model="user.age"><br>
    <button @click="sendData">save</button>
  </div>`,
  mixins: [mixin],
  methods: {
    sendData () {
      /*ajax send user data here*/
      setTimeout(_=>{
        /*false means edit complete,so that user list must be reloaded*/
        this.$emit('switch-edit-mode',false);
      },10)
    }
  }
});

var app = new Vue({
  el: '#app',
  data () {
    return {
      users: [],
      isModify: false,
      userId: null
    }
  },
  methods: {
    toggleModify (modify) {
       this.isModify = modify
       if(!modify){
          this.fetchUsers();
       }
    },
    fetchUsers () {
      /*load your user list data here*/
      this.users = data.map(o=>({
        id: o.id,
        name: o.name
      }))
    }
  },
  created () {
    this.fetchUsers()
  }
})
*{
    padding:0;
    margin:0;
  }
  ul,li{
    list-style:none;
  }
  .main{
    display: flex;
  }
  .user-list{
    width: 250px;
  }
  .user-list>li{
    border:1px solid skyblue;
    border-bottom: none;
  }
  .user-list>li:last-child{
    border-bottom:1px solid skyblue;
  }
  .content-wrapper{
    flex:1;
  }
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.16/dist/vue.js"></script>
<style>
  *{
    padding:0;
    margin:0;
  }
  ul,li{
    list-style:none;
  }
  .main{
    display: flex;
  }
  .user-list{
    width: 250px;
  }
  .user-list>li{
    border:1px solid skyblue;
    border-bottom: none;
  }
  .user-list>li:last-child{
    border-bottom:1px solid skyblue;
  }
  .content-wrapper{
    flex:1;
  }
</style>
<div id="app">
  <div class="main">
    <ul class="user-list">
      <li v-for="user in users" @click="userId=user.id">{{user.name}}</li>
    </ul>
    <div class="content-wrapper">
      <component v-if="userId" :is="isModify?'user-editor':'user-viewer'" @switch-edit-mode="toggleModify" :user-id="userId"></component>
      <div v-else>please choose a user to view or edit</div>
    </div>
  </div>
</div>

【讨论】:

  • 哦,该死的儿子,这对像我这样的人来说是无价的。让我剖析一下,然后回到一些潜在的问题。图例
  • 我的组件在不同的文件中,例如 import staffDetails from '../components/SidebarStaffDetails' ( components: {staffDetails}。我可以将我的 mixin 作为道具传递给该文件吗?也许问题“导入组件和组件模板方式有什么区别?”
【解决方案2】:

你的 mixin 文件:(mixin.js)

export default{
  props: {
    userId: {
      required: true
    }
  },
  data () {
    return {
      user: {}
    }
  },
  methods: {
    loadUser () {
      /*ajax to get user detail data here*/
      setTimeout(_=>{
        this.user = data.filter(o=>o.id==this.userId)[0]
      },10)
    }    
  },
  created () {
    this.loadUser()
  },
  watch: {
    userId (newVal) {
      if(newVal){
        this.loadUser()
      }
    }
  }
}

用法:

import mixin from 'mixin.js'
export default{
  ...
  mixins: [mixin]
}

【讨论】:

    猜你喜欢
    • 2019-04-16
    • 1970-01-01
    • 2017-06-13
    • 2013-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-20
    相关资源
    最近更新 更多