标签: vue-props

直接从 vue 3 设置获取的 props 不是响应式的

我正在 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)

vue.js reactive vue-props vuejs3

27
推荐指数
3
解决办法
6万
查看次数

如何在 Vue 2 中的 props 更改后重新渲染组件?

我的组件很简单:

<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)

vue.js vue-component vuejs2 vue-props

13
推荐指数
2
解决办法
2万
查看次数

Vue 组合 API 中的只读目标

我的“Generateur”组件正在向我的“Visionneuse”组件发送道具。浏览器中一切正常,但我在控制台中看到以下消息:

\n
Set 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)

vue.js vue-reactivity vue-props vuejs3 vue-composition-api

13
推荐指数
2
解决办法
2万
查看次数

How to set types to vue slot props Typescript

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

types typescript vue.js vue-props vuejs3

13
推荐指数
2
解决办法
1万
查看次数

我怎样才能在composition api 中观看一个prop?

当 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)

此代码收到错误:无效的监视源...我如何侦听道具的更新?

希望有人可以帮助我!:-)

vue.js vue-props vuejs3 vue-composition-api

11
推荐指数
1
解决办法
2万
查看次数

Vue &lt;脚本设置&gt;,在不导入的情况下无法使用defineProps和defineEmits

根据官方文档

defineProps编译器宏只能defineEmits在. 它们不需要导入,并且在处理时被编译掉。<script setup><script setup>


问题定义

如果不导入它,我就无法使用definePropsand defineEmitsin <script setup>。请参阅下面所附的错误屏幕截图。

如果不将其导入,则无法使用defineProps


我正在执行的 vue 代码
<!-- 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

10
推荐指数
1
解决办法
2万
查看次数

如何将 prop 从页面传递到布局

我目前有重复的布局,唯一的区别是我传递给组件的道具:

默认.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布局?

我知道我可以在已安装的情况下发出一些事件,但希望防止为此使用生命周期挂钩

vue.js nuxt.js vue-props

10
推荐指数
1
解决办法
6531
查看次数

Vue 3:如何使用组合 API 正确更新组件 props 值?

我有这样的组件:

<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)

我尝试使用方法更新 propcoordsupdateCoords,但收到错误消息:

未捕获的类型错误:无法设置未定义的属性(设置“坐标”)

在我的情况下如何正确更新 props 值?

javascript vue.js vue-component vue-props vuejs3

9
推荐指数
1
解决办法
2万
查看次数

Vue JS 非道具属性和禁用属性继承

我已阅读文档超过 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)

javascript vue.js vue-props

7
推荐指数
1
解决办法
5592
查看次数

为 Vue props 创建类型的正确方法是什么

我正在尝试type在 Vue js 中为我的 prop 创建自定义,我创建了一个 types 文件夹并将其添加到tsconfig.typeRootsIntelliSense 中,所有其他内容都正常工作,编译时没有问题,但是当我访问该组件时,我收到一个错误,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)

typescript vue.js typescript-typings nuxt.js vue-props

7
推荐指数
1
解决办法
1万
查看次数