【发布时间】:2019-04-12 08:54:21
【问题描述】:
我非常需要你的帮助... 我有一个 Vue 组件和端点,它为我提供了一个带有小菜单的脚本,并且该脚本包含一些操作。 所以,在我的脚本加载后,它的动作在页面上不起作用,我不知道为什么。 示例代码下方:
<template>
<div id="menu"></div>
</template>
<script>
export default {
name: 'HelloWorld',
data () {
return {
msg: 'Welcome to Your Vue.js App'
}
},
mounted () {
// here I get a script which contains menu bar with actions
// css and styles
this.$http.get("https://endpointwithscript.com/menu").then(response => {
if (response.status === 200) {
//in that case script doesn't work. Eg click
var div = document.getElementById("menu")
div.innerHTML += response.body;
document.appendChild(div);
}
})
}
}
</script>
如果我以这种方式插入我下载的脚本:
mounted () {
this.$http.get("https://endpointwithscript.com/menu").then(response => {
if (response.status === 200) {
document.write(response.body);
}
})
}
然后脚本工作,但另一个 html 元素被该脚本覆盖并且不显示。 如何下载脚本,将其插入页面并保留所有功能? 谢谢!
【问题讨论】:
-
该脚本包含什么?
-
@BoussadjraBrahim 标签
-
这样您就可以使用该内容构建一个组件,导入它并在此代码中使用它
-
@BoussadjraBrahim 是的,这是个好主意。但是如何正确构建它。我可以使用 var div = document.getElementById("menu") div.innerHTML += response.body; document.appendChild(div);但随后操作不起作用
-
@BoussadjraBrahim 我明天可以提供。会好吗?
标签: javascript html web vue.js vue-component