首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Vue 3传送到元素并替换内容

Vue 3传送到元素并替换内容
EN

Stack Overflow用户
提问于 2020-12-02 15:33:19
回答 1查看 701关注 0票数 2

我正在使用vue 3 teleport来呈现div中的元素,并且它的工作方式与预期的一样。

远程传送会将项目添加到所选元素,但不会替换div中的当前元素。

我如何设置远程传输来替换div中的元素,就像vue应用程序在挂载时替换锚定div内容一样?

上下文:我需要搜索引擎优化的原因占位符元素,直到应用程序渲染和传送。

代码语言:javascript
复制
<!-- HTML outside the app -->
<div>
  <div id="teleport-here">Teleport placeholder... I want this to be replaced by the teleport component</div>
  <div id="app">This placeholder will be replaced with app when app mounts...</div>
</div>

<!-- teleport component -->
<teleport to="#telport-here">
  <div>Teleport content...</div>
</teleport>
EN

回答 1

Stack Overflow用户

发布于 2020-12-02 16:28:08

您需要一些单独的逻辑,以便在必要时删除占位符。

setup中将占位符的内容设置为空

代码语言:javascript
复制
Vue.createApp({
  setup() {
    const placeholder = document.getElementById('teleport-here');

    placeholder.innerHTML = '';
  }
}).mount('#app');
代码语言:javascript
复制
<script src="https://unpkg.com/vue@next"></script>

<div>
  <div id="teleport-here">Teleport placeholder... I want this to be replaced by the teleport component</div>
  <div id="app">This placeholder will be replaced with app when app mounts...
    <!-- teleport component -->
    <teleport to="#teleport-here">
      <div>Teleport content...</div>
    </teleport>
  </div>
</div>

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65103910

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档