admin管理员组文章数量:1405324
I have the following 2 ponents
BrewTitle.vue
<template>
<h1>{{ title }}</h1>
</template>
<script>
export default {
data() {
return {
title: "Brew Title"
};
},
created() {
console.log("title created")
}
};
</script>
Snackbar.vue
<template>
<h1>{{ title }}</h1>
</template>
<script>
export default {
data() {
return {
title: "Brew Title"
};
},
created() {
console.log("snackbar created")
}
};
</script>
How they are added to the index.js file
import Vue from "vue";
import BrewTitle from "./ponents/BrewTitle";
import Snackbar from "./ponents/Snackbar";
Vueponent("brewtitle", BrewTitle);
Vueponent("snackbar", Snackbar);
const app = new Vue({
el: "#app"
});
In my html template I have the following snippet
<div id="app">
<brewtitle />
<snackbar />
</div>
<script src="main.js"></script>
The ponents are almost identical, but the snackbar is nowhere to be found on the html page or in the view browser extension. There are no problems with webpack and there is no message in the browser.
What am I doing wrong?
I have the following 2 ponents
BrewTitle.vue
<template>
<h1>{{ title }}</h1>
</template>
<script>
export default {
data() {
return {
title: "Brew Title"
};
},
created() {
console.log("title created")
}
};
</script>
Snackbar.vue
<template>
<h1>{{ title }}</h1>
</template>
<script>
export default {
data() {
return {
title: "Brew Title"
};
},
created() {
console.log("snackbar created")
}
};
</script>
How they are added to the index.js file
import Vue from "vue";
import BrewTitle from "./ponents/BrewTitle";
import Snackbar from "./ponents/Snackbar";
Vue.ponent("brewtitle", BrewTitle);
Vue.ponent("snackbar", Snackbar);
const app = new Vue({
el: "#app"
});
In my html template I have the following snippet
<div id="app">
<brewtitle />
<snackbar />
</div>
<script src="main.js"></script>
The ponents are almost identical, but the snackbar is nowhere to be found on the html page or in the view browser extension. There are no problems with webpack and there is no message in the browser.
What am I doing wrong?
Share Improve this question asked Jul 8, 2019 at 19:29 kristian nissenkristian nissen 2,9175 gold badges46 silver badges71 bronze badges 2- Do you see either of the "... created" console messages? – Phil Commented Jul 8, 2019 at 23:39
- There is no message in the console – kristian nissen Commented Jul 9, 2019 at 7:59
1 Answer
Reset to default 5Browsers don't support self-closing tags like these:
<brewtitle />
<snackbar />
Try having explicit closing tags instead:
<brewtitle></brewtitle>
<snackbar></snackbar>
If you use a self-closing tag for a ponent then the browser will just treat it as an opening tag. An implicit closing tag will be created when the parent element closes. That'll work fine if there are no other siblings but it will go wrong when there are.
So taking your original code as an example:
<div id="app">
<brewtitle />
<snackbar />
</div>
The <brewtitle>
won't count as closed until it reaches the closing </div>
. So this is equivalent to:
<div id="app">
<brewtitle>
<snackbar></snackbar>
</brewtitle>
</div>
So <snackbar>
will be treated as a child of <brewtitle>
. As brewtitle
doesn't have a slot the snackbar
will just be discarded.
This only applies if the HTML is being parsed directly by the browser. For anything parsed by Vue itself, such as in your .vue
files, this won't be a problem.
From the official Vue documentation, https://v2.vuejs/v2/style-guide/#Self-closing-ponents-strongly-remended
Components with no content should be self-closing in single-file ponents, string templates, and JSX - but never in DOM templates.
...
Unfortunately, HTML doesn’t allow custom elements to be self-closing - only official “void” elements.
本文标签: javascriptVue component not mounting or rendering and no error messagesStack Overflow
版权声明:本文标题:javascript - Vue component not mounting or rendering and no error messages - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744333561a2601077.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论