vue3 components
时间: 2023-09-12 13:02:57 浏览: 83
Vue 3 introduces a more streamlined way of creating components compared to previous versions. The new Composition API allows you to define components using functions instead of options objects.
To create a component in Vue 3, you can use the `defineComponent` function provided by the Vue package. Here's an example:
```javascript
import { defineComponent } from 'vue';
export default defineComponent({
name: 'MyComponent',
data() {
return {
message: 'Hello, Vue 3!',
};
},
methods: {
handleClick() {
console.log('Button clicked!');
},
},
template: `
<div>
<p>{{ message }}</p>
<button @click="handleClick">Click me</button>
</div>
`,
});
```
In the above example, we define a component called `MyComponent`. It has a `data` function that returns an object containing the `message` property. The component also has a `handleClick` method that logs a message to the console when the button is clicked.
The component's template is defined using the `template` option, where we can use Vue's template syntax to render dynamic data and handle events.
Once the component is defined, you can use it in your Vue application like any other component:
```html
<template>
<div>
<h1>My App</h1>
<my-component></my-component>
</div>
</template>
<script>
import MyComponent from './MyComponent.vue';
export default {
components: {
MyComponent,
},
};
</script>
```
In the above
阅读全文