无法将关联数组传递给 vue

Posted

技术标签:

【中文标题】无法将关联数组传递给 vue【英文标题】:Can't pass associative array to vue 【发布时间】:2019-03-03 05:31:07 【问题描述】:

我有这样的数组。

$array = ['a' => 1, 'b' => 2];

我想把它传递给 Vue 但它不能。

<my-component :array="'!! json_encode($array) !!'"
></my-component>

但输出在 html 中是这样显示的。

":1,"b":2'"="">

【问题讨论】:

Vue.js 无法处理关联数组,所以我看不出这样做的目的。你应该以 Vue.js 可以理解的形式引入它。 【参考方案1】:

这样使用

$array = ["a" => 1, "b" => 2];

<my-component :assoc-array='!! json_encode($array) !!'></my-component>

【讨论】:

但它得到的是字符串而不是对象。 我在运行时得到了这个"a":1,"b":2 好吧,我的错误是在已经运行 new Vue 的 HTML 元素旁边运行 new Vue,这就是为什么不起作用。

以上是关于无法将关联数组传递给 vue的主要内容,如果未能解决你的问题,请参考以下文章