我正在 vuejs 中编写一个应用程序,我想将 prop 传递给子组件,但我收到此错误:
props
从的根范围获取值setup()
将导致该值失去反应性
父组件
<template>
<div>
<course-list :courseId = "id" />
</div>
</template>
Run Code Online (Sandbox Code Playgroud)
import {useRoute} from 'vue-router';
import { ref, onMounted, reactive} from 'vue';
export default defineComponent({
components: { NavBar, CourseList, CourseContent },
props:{
courseId: String
},
setup(){
const route = useRoute()
const id = route.params.id
console.log("the course id is", id);
return{
id
}
}
}
Run Code Online (Sandbox Code Playgroud)
子组件
export default defineComponent({
components: { CourseTopic },
props: {
courseId: {
type: String
}
},
setup(props) { …
Run Code Online (Sandbox Code Playgroud) 我的组件很简单:
<template>
<btn :color="color" @click="toggleColor">{{btnmsg}}</btn>
</template>
<script>
import { Btn } from 'chico'
export default {
name: 'AButton',
components: {
Btn
},
data() {
return {
btnmsg: 'Legia pany',
colors: [
'blue', 'black', 'green',
'orange', 'teal', 'cyan',
'yellow', 'white'
],
color: 'red'
}
},
methods: {
toggleColor() {
this.color = this.colors[Math.floor(Math.random() * Math.floor(this.colors.length))];
}
}
}
</script>
Run Code Online (Sandbox Code Playgroud)
ChicoFamily 的内容<Btn>
是这样的:
<template>
<button :is="tag" :class="[className, {'active': active}]" :type="type" :role="role">
<slot></slot>
</button>
</template>
<script>
import classNames from 'classnames';
export default { …
Run Code Online (Sandbox Code Playgroud) 我的“Generateur”组件正在向我的“Visionneuse”组件发送道具。浏览器中一切正常,但我在控制台中看到以下消息:
\nSet operation on key "texteEnvoye" failed: target is readonly.\n
Run Code Online (Sandbox Code Playgroud)\n我真的不知道为什么会收到此消息,因为我将道具传递给了引用。\n这是我的组件:\n“Generateur”
\n<template>\n <div>\n <h1>G\xc3\xa9n\xc3\xa9ration d'une carte de voeux</h1>\n <div class="board">\n <Visionneuse :texteEnvoye="texte" :taille="0.5"/>\n </div>\n <textarea\n :style="'width: 60%; resize:none;height: 100px;'"\n v-model="texte"\n placeholder="\xc3\x89crivez votre texte ici">\n </textarea>\n </div>\n <div>\n <button\n v-if="lien.length == 0"\n id="boutonObtenirLien"\n v-bind:class="{ enCours: lienEnCours }"\n class="btn first"\n @click="obtenirLien">Obtenir le lien</button>\n <p\n v-if="lien.length > 0">\n Votre carte de voeux est accessible au lien suivant:<br/>\n <a :href="lien">{{ lien }}</a>\n </p>\n </div>\n</template>\n\n<script>\nimport Visionneuse from '@/components/Visionneuse.vue';\n\nimport axios from 'axios';\n\nimport {\n …
Run Code Online (Sandbox Code Playgroud) I'm trying to set types on my slot props to handle in a table component as you can see in the image
I also have been trying with
#body={item: UserItem}
, but it is only rename the parametter.
#body={<UserItem>item}
and #body={item<UserItem>}
, but it does not work
当 prop 更新时如何调用函数?
父容器:
<div>
<Maintable :type="typeRef" :country="countryRef" />
</div>
Run Code Online (Sandbox Code Playgroud)
子容器:
export default{
props: ['type'],
setup(props)
{
watch(props.type, () => {
console.log('hello')
})
}
Run Code Online (Sandbox Code Playgroud)
此代码收到错误:无效的监视源...我如何侦听道具的更新?
希望有人可以帮助我!:-)
根据官方文档,
defineProps
和编译器宏只能defineEmits
在. 它们不需要导入,并且在处理时被编译掉。<script setup>
<script setup>
如果不导入它,我就无法使用defineProps
and defineEmits
in <script setup>
。请参阅下面所附的错误屏幕截图。
<!-- HelloWorld.vue -->
<template>
<h1>{{ props.message }}</h1>
</template>
<script setup>
// import { defineProps } from 'vue';
const props = defineProps({
message: {
type: String,
required: true,
}
});
</script>
Run Code Online (Sandbox Code Playgroud)
视图 | ^3.2.6 (3.2.19) |
---|---|
vue-cli | @vue/cli 5.0.0-beta.4 |
节点: | v14.16.1 |
新项目管理 | 2012年6月14日 |
vue-props vuejs3 vue-composition-api vue-sfc vue-script-setup
我目前有重复的布局,唯一的区别是我传递给组件的道具:
默认.vue
<template>
<div class="page">
<SkipToContent />
<Header />
<Nuxt />
<Footer />
</div>
</template>
Run Code Online (Sandbox Code Playgroud)
前端.vue
<template>
<div class="page">
<SkipToContent />
<Header light="true" />
<Nuxt />
<Footer />
</div>
</template>
Run Code Online (Sandbox Code Playgroud)
有没有什么方法可以将其light: true
从页面传递到布局,以便我只能使用default.vue
布局?
我知道我可以在已安装的情况下发出一些事件,但希望防止为此使用生命周期挂钩
我有这样的组件:
<template>
<div>
<p>Current coords: <strong>{{ coords }}</strong></p>
<button type="button" @click="updateCoords">
</div>
</template>
<script>
export default {
props: {
coords: {
type: Array,
required: true
}
},
setup(props) {
const updateCoords = () => {
props.coords = [38.561785, -121.449756]
// props.coords.value = [38.561785, -121.449756]
}
return { updateCoords }
},
}
</script>
Run Code Online (Sandbox Code Playgroud)
我尝试使用方法更新 propcoords
值updateCoords
,但收到错误消息:
未捕获的类型错误:无法设置未定义的属性(设置“坐标”)
在我的情况下如何正确更新 props 值?
我已阅读文档超过 5 次,但我仍然不明白禁用属性继承的用途是什么,我不明白给出的示例。
我了解 props 的工作原理,它将数据从父组件传递到子组件。
父.vue
<template>
<div id="app">
<Child :num="num"></Child>
</div>
</template>
<script>
import Child from "@/components/Child";
export default {
name: "App",
components: {
Child
},
data() {
return {
num: 42
};
}
};
</script>
Run Code Online (Sandbox Code Playgroud)
儿童.vue
<template>
<div>
<h2>Child</h2>
<h4>num is {{num}}</h4>
<div id="total">
<h4>total is {{total}}</h4>
</div>
</div>
</template>
<script>
export default {
name: "Child",
props: {
num: {
type: Number,
default: 100
}
},
data() {
return {
};
},
computed: {
total() {
return …
Run Code Online (Sandbox Code Playgroud) 我正在尝试type
在 Vue js 中为我的 prop 创建自定义,我创建了一个 types 文件夹并将其添加到tsconfig.typeRoots
IntelliSense 中,所有其他内容都正常工作,编译时没有问题,但是当我访问该组件时,我收到一个错误,Car is not defined
但我已经定义了它并且它可以在其他地方工作,但在检查官方文档后,我知道 prop 需要 a constructor
所以我重新定义了类型declare class Car
并添加了构造函数原型,但又出现了同样的问题。
以下是文件: 汽车组件
<script lang="ts">
import Vue from "vue";
export default Vue.extend({
name: "the-car",
props: {
car: {
required: true,
type: Car,
},
},
});
</script>
Run Code Online (Sandbox Code Playgroud)
声明types/common/index.d.ts
文件
declare class Car {
name: String;
image: String;
kms: Number;
gears: String;
desc: String;
fuel: String;
engine: String;
price: Number;
constructor(name: String, image: String, kms: …
Run Code Online (Sandbox Code Playgroud) vue-props ×10
vue.js ×9
vuejs3 ×6
javascript ×2
nuxt.js ×2
typescript ×2
reactive ×1
types ×1
vue-sfc ×1
vuejs2 ×1