gpt4 book ai didi

javascript - 将 Django 数据传递给 Vue 组件

转载 作者:行者123 更新时间:2023-12-05 09:11:08 27 4
gpt4 key购买 nike

我正在使用 Django 作为后端,我正在尝试将一些数据传递到我制作的 Vue 表格组件中。我使用这个 tutorial 设置它该组件在使用 webpack 时显示正常。我正在尝试将数据设置为 javascript 常量,然后将其传递到组件中。数据似乎没有通过。这是我的脚本的布局方式。

index.html

{% block content %}
<script>
const gridData = {{json_data|safe}};
console.log(gridData)
</script>

<div id="app">
<table_component
v-bind:tableData=this.gridData
>
</table_component>

</div>
{% end block %}

myComponent.vue 脚本部分

export default {
name: 'table_component',
props: {
tableData: Array
},
data() {
return {

}
},
components: {
Grid,
ButtonModal
},
created(){
console.log(this.tableData)
},
}

当我查看控制台时,它没有显示任何数据。它只是说未定义。

view.py

class CurrentClassroomView(LoginRequiredMixin, CreateView):
template_name = 'home/index.html'

def get(self, request, *args, **kwargs):
db_data = list(myData.objects.all().values())
my_json_data = json.dumps(db_data)
return render(request, self.template_name, {'json_data':my_json_data})

我在控制台中收到这个消息,我不确定这意味着什么,也不知道为什么它把所有东西都变成小写,即使我用大写字母传递它也是如此。

[Vue tip]: Prop "griddata" is passed to component <Anonymous>, but the declared prop name is "gridData". Note that HTML attributes are case-insensitive and camelCased props need to use their kebab-case equivalents when using in-DOM templates. You should probably use "grid-data" instead of "GridData".
tip @ vue.js:639
extractPropsFromVNodeData @ vue.js:2294
createComponent @ vue.js:3233
_createElement @ vue.js:3427
createElement @ vue.js:3359
vm._c @ vue.js:3496
eval @ VM1553:3
Vue._render @ vue.js:3550
updateComponent @ vue.js:4066
get @ vue.js:4477
Watcher @ vue.js:4466
mountComponent @ vue.js:4073
Vue.$mount @ vue.js:9043
Vue.$mount @ vue.js:11943
Vue._init @ vue.js:5011
Vue @ vue.js:5077
eval @ index.js:14
./assets/js/index.js @ app.js:409
__webpack_require__ @ app.js:20
(anonymous) @ app.js:84
(anonymous) @ app.js:87

如有任何帮助,我们将不胜感激。我不确定我哪里出错了

最佳答案

当您设置 gridData 时并尝试将它绑定(bind)到它不工作的 Vue 组件,因为它们具有不同的上下文。当你处理 Vue 组件时,你只能访问 Vue 实例中定义的数据。此外,{{json_data|safe}}容易受到 XSS 类型的攻击。<​​/p>

但是,将数据从 Django 安全地传递到 Vue 组件非常容易,我们只需要使用 json_script 在 Vue 的 mounted 中过滤并加载数据钩子(Hook)。

views.py 中只需将列表传递给模板,无需使用json.dumps() :

class CurrentClassroomView(LoginRequiredMixin, CreateView):
template_name = 'home/index.html'

def get(self, request, *args, **kwargs):
db_data = list(myData.objects.all().values())
return render(request, self.template_name, {'json_data':db_data})

index.html 中,我们将使用 json_script用于创建 json_data 的 JSON 格式表示的模板标记变量:

{% block content %}
{{ json_data|json_script:"json_data" }}

<div id="app">
<table_component></table_component>
</div>
{% end block %}

这将创建一个 <script> block ,例如:

<script id="json_data" type="application/json">{"hello": "world"}</script>

最后,在 myComponent.vue 中,我们加载、JSON 解码并在 mounted 中设置数据钩子(Hook):

export default {
data() {
return {
tableData
}
},
components: {
Grid,
ButtonModal
},
mounted() {
this.tableData = JSON.parse(document.getElementById('json_data').textContent)
},
}

关于javascript - 将 Django 数据传递给 Vue 组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60460443/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com