How to render a list of static content with Vue named slot?

前端 未结 3 680
迷失自我
迷失自我 2021-02-02 03:05

I have trouble figuring out how to get the following to work:

My parent template


  link 1
  

        
3条回答
  •  我在风中等你
    2021-02-02 03:52

    This is easily accomplished with a render function.

    Vue.component("comp", {
      render(h){
        let links = this.$slots.links.map(l => h('li', {class: "comp-item"}, [l]))
        return h('ul', {class: 'comp'}, links)
      }
    })
    

    Here is a working example.

    console.clear()
    
    Vue.component("comp", {
      render(h){
        let links = this.$slots.links.map(l => h('li', {class: "comp-item"}, [l]))
        return h('ul', {class: 'comp'}, links)
      }
    })
    
    new Vue({
      el: "#app"
    })
    
    

    Or with the help of a small utility component for rendering vNodes you could do it like this with a template.

    Vue.component("vnode", {
      functional: true,
      render(h, context){
        return context.props.node
      }
    })
    
    Vue.component("comp", {
      template: `
        
    ` }) new Vue({ el: "#app" })
    
    

提交回复
热议问题