【问题标题】:How to let a VueJS component work only as a wrapper instead of having control of all the html?如何让 VueJS 组件仅作为包装器工作,而不是控制所有 html?
【发布时间】:2023-03-27 22:45:01
【问题描述】:

换句话说,某个时候可能会有一个看起来像这样的 html:
<bloglink blogposturl="http://a.link" blogposttitle="my title" ></bloglink>

模板属性如下:
...
template: '<h3><i><a style="text-decoration: underline; cursor: pointer;" v-on:click="load_blog_page(blogposturl)">{{blogposttitle}}</a></i></h3>',
...

但是如果一个人想表达怎么办

<bloglink blogposturl="http://a.link" blogposttitle="my title" > 
  <p>.....complex html in here.... which is not dynamic....</p>
</bloglink>

在加载网页时希望保留此 html,而不是完全替换。

一种方法是获取这个复杂的 html 并将其作为参数插入,小心正确转义等,但这似乎不太优雅。

推荐的方式是什么?

【问题讨论】:

    标签: vue.js vuejs2 vue-component


    【解决方案1】:

    Vuecomponent slot.

    例如:

    父组件:

    <template>
        <div>
            <slot></slot>
        </div>
    </template>
    

    其他组件:

    <template>
        <div>
            <parent>
                <p>whatever</p>
            </parent>
        </div>
    </template>
    

    【讨论】:

      猜你喜欢
      • 2017-09-11
      • 2016-04-06
      • 1970-01-01
      • 2021-02-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多