vue作用域插槽实践

引言

我在练手的时候发现后端返回的数据可以通过两种方式渲染 (自己遇到的 可能你都会 哈哈哈)

后端传过来的数据函数


from django.http import JsonResponse
def record_detailed(request):

    all_record = models.Record.objects.all()
    lis = []

    for obj in all_record:
        lis.append({
            ‘username‘: obj.username,
            ‘task_name‘: obj.task_name,              # 我想要serializers 但是发现效果不好       ‘task_status‘: obj.get_task_status_display(),        ‘task_type‘: obj.get_task_type_display(), })     return HttpResponse(json.dumps(lis))

数据

[{
    "username": "xiao",
    "task_name": "\u7533\u8bf7",
    "task_status": "\u672a\u5b8c\u6210",
    "task_type": "\u666e\u901a\u4efb\u52a1"
}]

前端页面

<template>
    <div style="min-height: 578px;" class=‘content-wrapper‘>
        <div>
            <h3>son2页面</h3>
        </div>
        <el-card class="box-card">
            <div slot="header" class="clearfix">
                <span>任务详细</span>
            </div>
            <el-row :gutter="7">
                <el-col>
                    <el-table :data="tableData" style="width: 100%" border="1" stripe>

                        <el-table-column type="index" label="#"></el-table-column>

                        <el-table-column prop="username" label="姓名" width="180"></el-table-column>

                        <el-table-column prop="task_name" label="任务名称"></el-table-column>

                        <el-table-column prop="task_status" label="任务状态"></el-table-column>
               // 第一种方式
                        <!-- <el-table-column prop="task_type" label="任务类型"></el-table-column> -->
                        //  第二种方式
                        <el-table-column label="任务类型">
                            <template slot-scope="scope">
                                {{scope.row.task_type}}                      {{scope.row}}   会出现这一行所有的数据
                            </template>
                        </el-table-column>

                    </el-table>
                </el-col>
            </el-row>
        </el-card>
    </div>
</template>

<script>
    export default {
        data() {
            return {
                tableData: []
            }
        },
        created() {
            this.getTableData()
        },
        methods: {
            getTableData() {
                this.$axios.get(‘http://127.0.0.1:8000/record_detailed/‘)
                    .then((response) => {
                        this.tableData = response.data
                    }).catch((error) => {

                    })
            }
        }
    }
</script>

<style>
</style>

原文地址:https://www.cnblogs.com/a438842265/p/12207056.html

时间: 2024-08-30 17:59:00

vue作用域插槽实践的相关文章

细说Vue作用域插槽,匹配应用场景。

最近在官方文档中看到,vue新增了一种插槽机制,叫做作用域插槽.要求的版本是2.1.0+. 首先来说一下:顾名思义,所谓作用域插槽,主要就在作用域,需要注意的是(以下几点看不懂不要紧,配合下面的例子,你会一看就懂): 1. 组件中的slot标签只能有有一个,而这一个slot用于替代组件调用时的多个标签.即一个slot代替一组范围的标签,即为作用域. 2. 作用域插槽的特殊在于:可在上层作用域中通过临时变量拿到组件定义时通过作用域插槽传递的数据. 3. 作用域插槽的技巧在于:可在上层作用域中通过拿

[干货]关于vue作用域插槽的新的深入理解

父级组件 <template> <div class="wrapper"> <son1 title="标题3" :content="listData3" @father="teClick"> <template v-slot="scope"> <b class="qianz">{{scope.item.prefix ? '有前缀

Vue作用域插槽:用作循环结构的模版

一 项目结构 二 App组件 <template> <div id="app"> <!-- 子组件 --> <todos :list="list" v-slot:default="{item}"> <!-- 插槽内容 --> <span v-if="item.isComplete">?</span> {{item.text}} </tod

新版vue作用域插槽的使用

2.6开始,作用域插槽的使用有了不同的地方: 作用域插槽的个人理解就是让子组件的数据可以在父组件中使用:  也是一个数据传递的方式了: 不多说,上代码 子组件定义一个插槽,并且定义一个需要传递到父组件的数据 html: <template> <div class="card-wrap"> <div class="foot"> <slot name="todo" v-bind:user="user

vue 作用域插槽

作用域插槽 作用域插件的目的就是:获取本组件的数据!. 示例代码todo-list组件: <ul> <li v-for="todo in filteredTodos" v-bind:key="todo.id" > <!-- 我们为每个 todo 准备了一个插槽, 将 `todo` 对象作为一个插槽的 prop 传入. --> <slot name="todo" v-bind:todo="todo

Vue组件之作用域插槽

写作用域插槽之前,先介绍一下Vue中的slot内容分发: 如果<child-component></child-component>标签之间没有插入那两个p标签的话,页面会显示子组件模板中定义的"<p>父组件如果没有插入内容,我将被显示</p>"这一则内容,但如果<child-component></child-component>标签之间有插入内容的话,则子组件模板中的<slot></slot&

Vue.js 源码分析(二十六) 高级应用 作用域插槽 详解

普通的插槽里面的数据是在父组件里定义的,而作用域插槽里的数据是在子组件定义的. 有时候作用域插槽很有用,比如使用Element-ui表格自定义模板时就用到了作用域插槽,Element-ui定义了每个单元格数据的显示格式,我们可以通过作用域插槽自定义数据的显示格式,对于二次开发来说具有很强的扩展性. 作用域插槽使用<template>来定义模板,可以带两个参数,分别是: slot-scope    ;模板里的变量,旧版使用scope属性 slot              ;该作用域插槽的nam

436 vue slot:插槽基本使用,具名插槽,作用域插槽

1. 插槽 : 替换内容 / 分发内容 (1)占位,像出口<router-view></router-view>. (2)没有新的内容放进来,就用默认的. (3)<slot></slot>将被替换成组件内的对应子节点. 2. 基本使用 <el-car> <div>宝马发动机</div> </el-car> 组件的内部 02-插槽的基本使用.html <!DOCTYPE html> <html

使用slot分发内容 作用域插槽

除非子组件模板包含至少一个<slot>插口,否则父组件的内容将会别丢弃.当子组件模板只有一个没有属性的slot时,父组件整个内容片断将插入到slot所在的DOM位置,并替换掉slot标签本身. 最初在 <slot> 标签中的任何内容都被视为备用内容.备用内容在子组件的作用域内编译,并且只有在宿主元素为空,且没有要插入的内容时才显示备用内容. 作用域插槽是一种特殊类型的插槽,用作使用一个(能够传递数据到)可重用模板替换已渲染元素. 在子组件中,只需将数据传递到插槽,就像你将 prop