【发布时间】:2016-06-15 11:40:26
【问题描述】:
我正在尝试使用道具将 JSON 对象向下传递到 Vue 组件中。 JSON 对象是使用 json_encode() 在 WordPress 查询上计算的,该查询收集页面的所有帖子。然后我使用 PHP 函数 addcslashes() 来转义我所有的双引号。
当我在这个变量上使用echo 时,这是输出:
{\"ID\":185,\"post_author\":\"1\",\"post_date\":\"2016-02-23 14:32:45\",\"post_date_gmt\":\"2016-02-23 14:32:45\"}
但是,当我将 JSON 字符串传递给我的 Vue 属性时,所有 Vue 调试器输出的都是 testprop: "{\\"。
知道为什么我无法在 Vue 属性中获取完整的 JSON 对象字符串吗?
$newsPostQuery = new WP_Query($args);
$posts = $newsPostQuery->posts[0];
$posts = json_encode($posts);
$posts = addcslashes($posts, '"');
echo "<pre>";
var_dump($posts);
echo "</pre>";
echo $posts;
?>
<testposts testprop="<?php echo $posts; ?>"
></testposts>
<script type="text/javascript">
new Vue({
el: '.News-Feed',
components: {
testposts: {
template: '#test-posts',
props: ['testprop'],
ready() {
console.log(this.testprop);
this.testprop = JSON.parse(this.testprop);
},
}
}
});
</script>
【问题讨论】:
-
是的,我试过不转义引号,但 Vue 道具只会吐出
{