简体   繁体   English

渲染 vue.js 组件并传入数据

[英]rendering vue.js components and passing in data

I'm having trouble figuring out how to render a parent component, display a list of contracts in a list on part of the page, and when a user clicks on one of them, display the details of that specific contract on the other part of the page.我无法弄清楚如何呈现父组件,在页面的一部分列表中显示合同列表,当用户单击其中一个时,在另一部分显示该特定合同的详细信息这页纸。

Here is my slim file:这是我的苗条文件:

#contracts_area
  .filter-section
     ul
       li.filter-item v-for="contract in contractsAry" :key="contract.id" @click="showContract(contract)"
        | {{ contract.name }}
  .display-section
    component :is="currentView" transition="fade" transition-mode="out-in"

script type="text/x-template" id="manage-contracts-template"
  div
    h1 Blank when page is newly loaded for now

script type="text/x-template" id="view-contract-template"
  div :apply_contract="showContract"
    h1#display-item__name v-name="name"

javascript: javascript:

Vue.component('manage-template', {
    template: '#manage-contracts-template'
  });

  Vue.component('view-contract', {
    template: '#view-contract-template',
    props: ['show_contract'],
    data: function() {
      return {
        name: ''
      }
    },
    methods: {
      showContract: function(contract) {
        return this.name = contract.name
      }
    }
  });

  Vue.http.headers.common['X-CSRF-Token'] = $('meta[name="csrf-token"]').attr('content');
  var contractsResource = Vue.resource('/all_contracts{/id}.json');

  var contracts = new Vue({
    el: '#contracts_area',
    data: {
      currentView: 'manage-template',
      contractsAry: [],
      errors: {}
    },
    mounted: function() {
      var that = this;
      contractsResource.get().then(
        function(res) {
          that.contractsAry = res.data;
        }
      )
    },
    methods: {
      showContract: function(contract) {
        this.currentView = 'view-contract'
      }
    }
  });

Basically I'd like it so that when a user clicks on any contract item in the .filter-section, it shows the data for that contract in the .display-section.基本上我希望这样当用户单击 .filter-section 中的任何合同项时,它会在 .display-section 中显示该合同的数据。 How can I achieve this?我怎样才能做到这一点?

In short you can bind a value to a prop .简而言之,您可以将值绑定到prop

.display-section
  component :is="currentView" :contract="currentContract"

view-contract视图合同

props: ['contract']

contracts-area合同区

data: {
  currentContract: null,
},
methods: {
  showContract: function(contract) {
    this.currentView = "view-contract";
    this.currentContract = contract;
  }
}

There are multiple ways to pass data in Vue.在 Vue 中传递数据有多种方式。

  1. Binding values to props .将值绑定到props
  2. Using ref to directly call a method from a child component.使用ref直接从子组件调用方法。
  3. Custom Events . 自定义事件 Note that to pass events globally, you will need a global event bus.请注意,要全局传递事件,您将需要一个全局事件总线。
  4. A single central source of truth (ie vuex )单一的中心事实来源(即vuex

I have illustrated methods 1, 2, 3 in Codepen我在Codepen中说明了方法 1、2、3

Note that 2 nd and 3 rd methods will only work after your component has been rendered.请注意,第 2第 3方法仅在您的组件被渲染后才有效。 In your case, since your components for currentView are dynamic and when user clicked, display-section component does not yet exists;在您的情况下,由于您的 currentView 组件是动态的,并且当用户单击时,显示部分组件尚不存在; it will not receive any events yet.它还不会收到任何事件。 So their content will be empty at first.所以他们的内容一开始是空的。

To workaround this you can directly access $parent in mounted() from child component, however this would create coupling between them.要解决此问题,您可以从子组件直接访问mounted()中的$parent ,但这会在它们之间产生耦合。 Another solution is creating the components but conditionally displaying them.另一种解决方案是创建组件但有条件地显示它们。 And one another solution would be waiting until child component has been mounted and then emitting events.另一种解决方案是等到子组件已安装,然后发出事件。

If your needs are simple I suggest binding values to props ( 1 ), else you may consider using something like vuex.如果您的需求很简单,我建议将值绑定到 props ( 1 ),否则您可以考虑使用 vuex 之类的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM