简体   繁体   English

如何将refs从父组件传递给子组件? 不使用 Typescript?

[英]How to pass refs from parent component to child component? Without using Typescript?

I have an App.vue with the property "clicked" that I'm trying to pass to child components.我有一个 App.vue,其属性为“clicked”,我正试图将其传递给子组件。

App.vue App.vue

<template>
  <NavBar :clicked="clicked"/>
  <BackDrop :clicked="clicked"/>
  <SideDrawer :clicked="clicked" />
  <router-view></router-view>
</template>
<script>
import { ref } from "vue";
import NavBar from "./components/Navbar/NavBar.vue";
import BackDrop from "./components/Backdrop/BackDrop.vue";
import SideDrawer from "./components/Sidedrawer/SideDrawer.vue";
export default {
  name: "App",
  components: { NavBar, BackDrop, SideDrawer },

  setup() {
    const clicked = ref(false);

    return { clicked };
  },
};
</script>
export default App;

so the child components can be rendered conditionally like:因此可以有条件地呈现子组件,例如:

SideDrawer.vue侧边抽屉.vue

<template v-if="clicked">
  <div class="sidedrawer"></div>
</template>
<script setup>
</script>

Did I pass the "clicked" ref properly in the "App.vue"?我是否在“App.vue”中正确传递了“clicked”引用?

If I did, how do you access them in the child component?如果我这样做了,你如何在子组件中访问它们? I've already googled and looked at a bunch of StackOverflow posts but the majority of them seem to use, "defineProps()", like this code:我已经用谷歌搜索并查看了一堆 StackOverflow 帖子,但其中大多数似乎都使用“defineProps()”,就像这段代码:

const props = defineProps({
  msg: String,
  user: Object,
});

// Destructuring props
let { name, lastname } = props.user;
</script>

but I'm not using Typescript so this won't work for me.但我没有使用 Typescript,所以这对我不起作用。

What is the "non-typescript" way of passing props?传递道具的“非打字稿”方式是什么? Do you need to use Typescript to pass props?传道具需要用Typescript吗?


EDIT:编辑:

Even when I implement Typescript and use this code:即使当我实施 Typescript 并使用此代码时:

SideDraw.vue侧画.vue

<template v-if="clicked">
  <div class="sidedrawer">
    <h1>{{ props.clicked }}</h1>
  </div>
</template>
<script setup>
const props = defineProps({
  clicked: boolean,
});
</script>

I get the errors:我收到错误:

'defineProps' is not defined.eslintno-undef
'boolean' is not defined.eslintno-undef

... ...

I gathered from this Github thread to set global variables so I did this, but it still doesn't work.我从这个Github 线程收集来设置全局变量所以我这样做了,但它仍然不起作用。

babel.config.js babel.config.js

module.exports = {
  presets: [
    '@vue/cli-plugin-babel/preset'
  ],
    globals: {
      defineProps,
      defineEmits,
      defineExpose,
      withDefaults
    }
  }
}

In JavaScript you have two common ways of defining props, either with just array of prop names like so:在 JavaScript 中,你有两种常见的定义道具的方法,要么只使用道具名称数组,如下所示:

const props = defineProps([ 'clicked', 'someOtherProp' ]);

Or much more preferred(due to saving between hours and days of troubleshooting) is to define each prop with object as so:或者更可取的(由于节省了数小时和数天的故障排除时间)是用 object 定义每个道具,如下所示:

  const props = defineProps({
    clicked: { type: Boolean, default: false, required: true },
  });

Here is the relevant section of docs: https://vuejs.org/guide/components/props.html#prop-validation这是文档的相关部分: https://vuejs.org/guide/components/props.html#prop-validation

No, you do not need TypeScript when defining props.不,定义道具时不需要 TypeScript。 TypeScript is a superset of JavaScript so any TypeScript is also valid JavaScript. TypeScript 是 JavaScript 的超集,因此任何 TypeScript 也是有效的 JavaScript。

When using <script setup> , you use defineProps to define which props a component can accept.使用<script setup>时,您可以使用 defineProps 来定义组件可以接受的道具。 You do not need to declare or import it anywhere as it is a compiler macro.您不需要在任何地方声明或导入它,因为它是一个编译器宏。 If ESlint is complaining, then it is not configured properly.如果 ESlint 报错,则说明它配置不正确。 Add the following to your eslint configuration file.将以下内容添加到您的 eslint 配置文件中。

module.exports = {
    env: {
        'vue/setup-compiler-macros': true
    }
}

In each of your child components you need to accept "clicked" as a property.在您的每个子组件中,您需要接受“clicked”作为属性。

<script setup>
const props = defineProps(['clicked'])
</script>

or (using runtime types - this is not Typescript as pointed out by @Estus);或(使用运行时类型——这不是@Estus 指出的 Typescript);

<script setup>
const props = defineProps({
  clicked: Boolean
})
</script>

In your App.vue, you need to pass a value to the prop as follows;在你的 App.vue 中,你需要传递一个值给 prop,如下所示;

<script setup>
import { ref } from "vue";

const clicked = ref(false);
</script>
<template>
    <NavBar :clicked="clicked"/>
    <BackDrop :clicked="clicked"/>
    <SideDrawer :clicked="clicked" />
    <router-view></router-view>
</template>

If you are not going to use <script setup> , then you can define which props to accept as follows;如果您不打算使用<script setup> ,那么您可以按如下方式定义要接受的道具;

export default {
  name: "NavBar",
  props: ['clicked']
};

or (using runtime types - this is not Typescript as pointed out by @Estus);或(使用运行时类型——这不是@Estus 指出的 Typescript);

export default {
  name: "NavBar",
  props: {
    clicked: Boolean
  }
};

You need to defineProps code in your child component and not in you app.vue.你需要在你的子组件中定义 Props 代码,而不是在你的 app.vue 中。 Also dont forget to use script setup so that you dont have to import defineprops.也不要忘记使用脚本设置,这样您就不必导入 defineprops。 from vue.从视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM