lax*_*xer 1 javascript django vue.js vue-component vuejs2
我使用 Django 作为后端,我试图将一些数据传递到我制作的 Vue 表组件中。我使用本教程进行设置使用 webpack 时组件显示正常。我正在尝试将数据设置为 javascript 常量,然后将其传递到组件中。数据似乎没有通过。这是我的脚本的布局方式。
索引.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 %}
Run Code Online (Sandbox Code Playgroud)
myComponent.vue 脚本部分
export default {
name: 'table_component',
props: {
tableData: Array
},
data() {
return {
}
},
components: {
Grid,
ButtonModal
},
created(){
console.log(this.tableData)
},
}
Run Code Online (Sandbox Code Playgroud)
当我查看控制台时,它没有显示任何数据。它只是说未定义。
查看.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})
Run Code Online (Sandbox Code Playgroud)
我在控制台中得到了这个,我不确定这意味着什么,以及为什么即使我用大写字母传递它,它也会把所有东西都变成小写。
[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
Run Code Online (Sandbox Code Playgroud)
对此的任何帮助将不胜感激。我不确定我哪里出错了
当您设置gridData并尝试将其绑定到 Vue 组件时,它不起作用,因为它们具有不同的上下文。当您处理 Vue 组件时,您只能访问 Vue 实例中定义的数据。此外,{{json_data|safe}}容易受到 XSS 类型的攻击。
但是,将数据从 Django 安全地传递到 Vue 组件非常容易,我们只需要使用json_script过滤器并将数据加载到 Vue 的mounted钩子中即可。
在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})
Run Code Online (Sandbox Code Playgroud)
在index.html 中,我们将使用json_script模板标签来创建一个 JSON 格式的json_data变量表示:
{% block content %}
{{ json_data|json_script:"json_data" }}
<div id="app">
<table_component></table_component>
</div>
{% end block %}
Run Code Online (Sandbox Code Playgroud)
这将创建一个<script>块,例如:
<script id="json_data" type="application/json">{"hello": "world"}</script>
Run Code Online (Sandbox Code Playgroud)
最后,在myComponent.vue 中,我们加载、JSON 解码并在mounted钩子中设置数据:
export default {
data() {
return {
tableData
}
},
components: {
Grid,
ButtonModal
},
mounted() {
this.tableData = JSON.parse(document.getElementById('json_data').textContent)
},
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1979 次 |
| 最近记录: |