gpt4 book ai didi

javascript - Vue 3 组件不呈现但显示在元素树上

转载 作者:行者123 更新时间:2023-12-05 04:38:35 26 4
gpt4 key购买 nike

我是 vue 的新手,我试图将我的一个组件更改为新语法以学习 vue 3。该组件在更改之前工作得很好,所以我一定遗漏了一些东西。

这是母组件:

<template>
<div class="user-profile">
<div>
<div class="user-profile__user-panel">
<h1 class="user-profile__username">@{{ user.username }}</h1>
<div v-if="user.isAdmin" class="user-profile__admin-badge">Admin</div>
<div class="user-profile__follower-count">
<string><b>Followers: </b></string> {{ followers }}
</div>
</div>
<NewTwoot @create-twoot="createNewTwoot" />
</div>
<div class="user-profile__twoots-wraper">
<TwootItem
v-for="twoot in user.twoots"
:username="user.username"
:key="twoot.id"
:twoot="twoot"
@favorit="toggleFavorite(id)"
/>
</div>
</div>
</template>

<script>
import TwootItem from "./TwootItem.vue";

export default {
name: "UserProfile",
components: {
TwootItem,
},
data() {
return {
followers: 0,
user: {
id: 1,
username: "GabrielBG",
firstName: "Gabriel",
lastName: "Gutierrez",
email: "gbg@scienceiscoll.com",
isAdmin: true,
twoots: [
{
id: 2,
content: "This is my second twoot",
},
{
id: 3,
content: "This is my third twoot",
},
{
id: 1,
content: "This is my first twoot",
},
],
},
};
},
watch: {
followers(newFollowerCount, oldFollowerCount) {
if (oldFollowerCount > newFollowerCount) {
console.log("You lost a follower!");
} else {
console.log("You gained a follower!");
}
},
},
computed: {
fullName() {
return this.user.firstName + " " + this.user.lastName;
},
},
methods: {
followUser() {
this.followers++;
},
toggleFavorite(id) {
console.log("Toggling favorite for twoot with id: " + id);
},
createNewTwoot(newTwootContent) {
this.user.twoots.unshift({
id: this.user.twoots.length + 1,
content: newTwootContent,
});
},
},
mounted() {
this.followUser();
},
};
</script>

这是经过重构但现在不呈现的组件:

<template>
<form class="create-twoot" @submit.prevent="createNewTwoot">
<lable for="new-twoot"
><strong>New Twoot</strong> ({{ newTwootCharCount }}/180)
</lable>
<textarea id="new-twoot" rows="5" v-model="state.newTwootContent" />
<div class="create-twoot-type">
<lable for="twoot-type">
<strong>Twoot Type</strong>
</lable>
<select id="twoot-type" v-model="state.selectedTwootType">
<option
:value="option.value"
v-for="(option, index) in state.twootTypes"
:key="index"
>
{{ option.name }}
</option>
</select>
</div>
<button
type="submit"
:disabled="
newTwootContent.length === 0 ||
newTwootContent.length > 180 ||
newTwootType == 'draft'
"
>
Twoot
</button>
</form>
</template>

<script>
import { reactive, computed } from "vue";
export default {
name: "NewTwoot",
setup(_props, ctx) {
const state = reactive({
newTwootContent: "",
selectedTwootType: "instant",
twootTypes: [
{ value: "draft", name: "Draft" },
{ value: "instant", name: "Instant Twoot" },
],
});

const newTwootCharCount = computed(() => state.newTwootContent.length);

function createNewTwoot() {
ctx.emit("create-twoot", state.newTwootContent);
state.newTwootContent = "";
}

return {
state,
newTwootCharCount,
createNewTwoot,
};
},
};
</script>

我可以在元素树上看到它,但它显示为 <newtwoot></newtwoot>就好像它是空的。

最佳答案

我看到两个错误:

  1. 您只在父项中导入 TwootItem 而不是 NewTwoot。这解释了为什么它没有正确呈现。
  2. 你没有在子组件中定义 emit,看我的回答: vue 3 emit warning " Extraneous non-emits event listeners"

因此导入缺少的组件 import NewTwoot from "./NewTwoot.vue"; 并将其添加到组件中应该可以解决问题:

components: {
NewTwoot,
TwootItem,
}

关于javascript - Vue 3 组件不呈现但显示在元素树上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70568893/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com