简体   繁体   中英

how to substitute named slot from parent to child to grandchild with Vuejs

I am faced with a situation to render a third party date picker and also get the value from the third party component. since it is a third party i can't define a prop on it. However, the third party component provides an ability to pass variable in v-model.

So the design of my components requires me to be able to pass named slot from parent to child to grandchild.

for example;

//============ enter-or-upload-task.js =============

Vue.config.productionTip = false;

const sourceoftruth = {orderdate:''};

Vue.component('upload-by-csv',{
    template:
    `<div><input type="file" /></div>
    `
});

//here i want to use the date picker

Vue.component('enter-task-by-form',{
    data:function(){
       return {
          sourceoftruth
       };
    },
    methods:{
       getOrderDate: function(){
          console.log(sourceoftruth.orderdate);
       }
    },
    template:
    `<div>
        <label>Order Date:</label>
        <!--Hoping to get datepicker component displayed here -->
        <slot name="picker"></slot>
        <input type="submit" @click.prevent = "getOrderDate()" />
     </div>
    `
});

const EnterTaskOrUploadByCSV = {
    template:
    `<div>
            <upload-by-csv></upload-by-csv>
            <enter-task-by-form>
               <!-- wish to pass this named slot to this component -->
               <slot name="picker"></slot>
            </enter-task-by-form>
    </div>
    `
 }

 new Vue({
   el:"#app",
   data:{
     sourceoftruth
   },
   components:{
      'datepicker':vuejsDatepicker,
      'enter-form-or-csv':EnterTaskOrUploadByCSV
   }
 })
 // ===========================================================

The index.html is

 <script src="./js/lib/vue.js></script>
 <script src="./js/lib/vuejs-datepicker.js"></script>

 <div id="app">
      <enter-form-or-csv>
         <datepicker v-model="sourceoftruth.orderdate" slot="picker"> 
         </datepicker>
      </enter-form-or-csv>
 </div>

 <script src = "./js/components/enter-or-upload-task.js"></script>

I have tried passing the named slot to the appropriate position but couldn't get it working. Please I need help on how to solve this.

Glad it's working now. The changes I made that it to work

in EnterTaskOrUploadByCSV i added a template slot to hold the datepicker component going to be received from the parent. then the grandchild component would be expecting the template named slot.

const EnterTaskOrUploadByCSV = {
template:
`<div>
        <upload-by-csv></upload-by-csv>
        <enter-task-by-form>
           <template slot="passpicker">
               <slot name="picker"></slot>
           </template>
        </enter-task-by-form>
</div>
`
}

The grandchild component expecting the template named slot.

Vue.component('enter-task-by-form',{
data:function(){
   return {
      sourceoftruth
   };
},
methods:{
   getOrderDate: function(){
      console.log(sourceoftruth.orderdate);
   }
},
template:
`<div>
    <label>Order Date:</label>
    <!-- Datepicker will show now -->
    <slot name="passpicker"></slot>
    <input type="submit" @click.prevent = "getOrderDate()" />
 </div>
`
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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