Vue技术19单文件组件

这个示例展示了如何在 Vue.js 中创建和使用单文件组件(School 和 Student)。每个组件包含模板、脚本和样式部分,用于定义结构、交互和外观。App.vue 文件将这两个组件导入并显示在页面上,而 main.js 和 index.html 文件则负责应用的初始化和渲染。
摘要由CSDN通过智能技术生成

在这里插入图片描述
School.vue

<template>
  <!-- 组件的结构 -->
    <div class="demo">
        <h2>学校名称:{{name}}</h2>
        <h2>学校地址:{{address}}</h2>
        <button @click="showName">点我提示学校名</button>
    </div>
</template>

<script>
    // 组件交互相关的代码(数据、方法等等)
    export default {
        name:'School',
        data(){
            return {
                name:'尚硅谷',
                address:'北京'
            }
        },
        methods: {
            showName(){
                alert(this.name)
            }
        }
    }
</script>

<style>
    /* 组件的样式 */
    .demo{
        background-color:orange;
    }
</style>

Student.vue

<template>
  <!-- 组件的结构 -->
    <div>
        <h2>学生姓名:{{name}}</h2>
        <h2>学生年龄:{{age}}</h2>
        <button @click="showName">点我提示学生名字</button>
    </div>
</template>

<script>
    // 组件交互相关的代码(数据、方法等等)
    export default {
        name:'Student',
        data(){
            return {
                name:'张三',
                age:18
            }
        },
        methods: {
            showName(){
                alert(this.name)
            }
        }
    }
</script>


App.vue

<template>
    <div>
        <School></School>
        <Student></Student>
    </div>
</template>

<script>
    //引入组件
    import School from './School.vue'
    import Student from './Student.vue'
import Student from './Student.vue';
    export default {
        name:'App',
        components:{
    School,
    Student,
    Student
}

    }
</script>


main.js

import App from './App.vue'

new Vue({
    el:'#root',
    template:`<App></App>`,
    components:{App}
})

index.html

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>练习一下单文件组件的语法</title>
</head>
<body>
    <!--准备一个容器-->
    <div id="root"></div>
    <script type="text/javascript" src="../js/vue.js"></script>
    <script type="text/javascript" src="./main.js"></script>
</body>
</html>
以下是一个简文件组件示例,用于创建一个表格组件并接收父组件传递的学生信息数组。在该示例中,我们使用了Vue 2.x版本。 ``` <template> <div> <table> <thead> <tr> <th>序号</th> <th>班级</th> <th>姓名</th> <th>性别</th> <th>年龄</th> </tr> </thead> <tbody> <tr v-for="(student, index) in students" :key="index"> <td>{{ index + 1 }}</td> <td>{{ student.class }}</td> <td>{{ student.name }}</td> <td>{{ student.gender }}</td> <td>{{ student.age }}</td> </tr> </tbody> </table> </div> </template> <script> export default { name: 'TableComponent', props: { students: { type: Array, required: true } } } </script> <style scoped> table { border-collapse: collapse; width: 100%; } th, td { border: 1px solid black; padding: 8px; text-align: center; } th { background-color: #ccc; } </style> ``` 在父组件中,我们可以将学生信息数组作为props传递给表格组件: ``` <template> <div> <table-component :students="students"></table-component> </div> </template> <script> import TableComponent from './TableComponent.vue' export default { name: 'ParentComponent', components: { TableComponent }, data() { return { students: [ { class: '1801', name: '张三', gender: '男', age: 18 }, { class: '1802', name: '李四', gender: '女', age: 19 } ] } } } </script> ``` 在这个示例中,我们已经成功地使用文件组件方式创建了一个表格组件,并通过props从父组件中传递学生信息数组。在表格组件中,我们使用了v-for指令来遍历学生信息数组,并将每个学生的信息显示在表格中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yitahutu79

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值