Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
haizhilin2013 opened this issue Jun 16, 2019 · 5 comments
Open

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

haizhilin2013 opened this issue Jun 16, 2019 · 5 comments
Labels
vue vue

Comments

@haizhilin2013
Copy link
Collaborator

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

@haizhilin2013 haizhilin2013 added the vue vue label Jun 16, 2019
@wenyejie
Copy link

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

@cainiao308
Copy link

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

@dongtianqi
Copy link

写过

@qq-radio
Copy link

qq-radio commented Jan 4, 2021

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

@Cai-zhiji
Copy link

自定义组件的写法

创建一个名为 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
Labels
vue vue
Projects
None yet
Development

No branches or pull requests

6 participants