Skip to content

[vue] 你有写过自定义组件吗? #260

Open
@haizhilin2013

Description

@haizhilin2013
Collaborator

[vue] 你有写过自定义组件吗?

Activity

wenyejie

wenyejie commented on Jun 17, 2019

@wenyejie

我自己写了一套公司UI/底层组件库

cainiao308

cainiao308 commented on Feb 26, 2020

@cainiao308

这咋回答呢?随便吹?我写了个cavascript?

dongtianqi

dongtianqi commented on Mar 28, 2020

@dongtianqi

写过

radio-qq

radio-qq commented on Jan 4, 2021

@radio-qq

写过,随便说点组件的引入问题、注册问题、传值问题吧

Cai-zhiji

Cai-zhiji commented on Jul 6, 2023

@Cai-zhiji

自定义组件的写法

创建一个名为 MyComponent.vue 的单文件组件:

<template>
  <div class="my-component">
    <h2>{{ title }}</h2>
    <p>{{ content }}</p>
    <button @click="onClick">Click me</button>
  </div>
</template>

<script>
export default {
  name: 'MyComponent',
  props: {
    title: String,
    content: String
  },
  methods: {
    onClick() {
      // 处理点击事件的逻辑
      console.log('Button clicked');
    }
  }
}
</script>

<style scoped>
.my-component {
  /* 组件的样式 */
}
</style>

在父组件中使用自定义组件:

<template>
  <div>
    <my-component title="Hello" content="Welcome to my custom component"></my-component>
  </div>
</template>

<script>
import MyComponent from './MyComponent.vue';

export default {
  components: {
    MyComponent
  }
}
</script>

在上述示例中,MyComponent.vue 是一个自定义组件,它包含了模板、样式和逻辑。在模板中,使用了双花括号 {{}} 来绑定属性值和显示数据。通过 @click 事件监听器绑定了一个点击事件。

在父组件中,通过引入 MyComponent 并在 components 选项中注册,就可以在模板中使用该自定义组件。通过设置组件的 props 属性,可以传递数据给自定义组件,并在组件内部使用。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @wenyejie@haizhilin2013@dongtianqi@cainiao308@radio-qq

        Issue actions

          [vue] 你有写过自定义组件吗? · Issue #260 · haizlin/fe-interview