【发布时间】:2021-07-01 01:37:57
【问题描述】:
我试图获取数据,但它说计算方法在 vue 开发工具中未定义 我的方法是
<script>
export default{
name:"about",
mounted(){
this.$store.dispatch('getFrontAbouts');
},
computed:{
about(){
return this.$store.getters.about;
}
},
}
</script>
我通过 axios 调用获取这些数据的 store2.js 文件
export default{
state: {
aboutData:[],
},
getters:{
about(state){
return state.aboutData;
},
},
actions:{
getFrontAbouts(data){
axios.get("get-front-about").then((response)=>{
data.commit('about',response.data.about);
}).catch((error)=>{
})
},
},
mutations: {
about(state,data){
return state.aboutData = data;
},
}
}
我正在获取数据的控制器文件
关于()的公共函数{
$about = About::where('publication_status',1)->orderBy('id','ASC')->take(1)->first();
return response()->json(['about',$about],200);
}
这是我的 vue 组件,正在执行计算方法
<div class="row topmargin bottommargin gutter-lg-50 align-items-center">
<div class="col-lg-12 p-lg-5">
<div class="heading-block border-bottom-0 mb-0">
<h2 class="nott font-weight-semibold mb-4 text-secondary" style="color: #1ABC9C;">Our Story</h2>
<p v-if="about">{{about.about_us}}</p>
<div class="row">
<div class="col-6 col-sm-6 mb-4">
<div class="counter color font-weight-semibold"><span data-from="1" data-to="3" data-refresh-interval="2" data-speed="600"></span>+</div>
<h5 class="mt-0 font-weight-medium">Branches</h5>
</div>
<div class="col-6 col-sm-6 mb-4">
<div class="counter color font-weight-semibold"><span data-from="1" data-to="37" data-refresh-interval="11" data-speed="900"></span>+</div>
<h5 class="mt-0 font-weight-medium">Single Studios</h5>
</div>
<div class="col-6 col-sm-6 mb-4">
<div class="counter color font-weight-semibold"><span data-from="1" data-to="21" data-refresh-interval="3" data-speed="1000"></span>+</div>
<h5 class="mt-0 font-weight-medium">Events per Month</h5>
</div>
<div class="col-6 col-sm-6 mb-4">
<div class="counter color font-weight-semibold"><span data-from="100" data-to="4500" data-refresh-interval="100" data-speed="1500"></span>+</div>
<h5 class="mt-0 font-weight-medium">Active Members</h5>
</div>
</div>
</div>
</div>
</div>
</div>
<div>
<div class="row justify-content-center topmargin-sm">
<div class="col-md-5 offset-md-1">
<h3 class="text-dark"><i class="icon-line-circle-check color mr-1 position-relative" style="top: 2px;color: #1ABC9C;"></i> Why do you choose DreamsEye?</h3>
<p v-if="about">{{about.choice_us}}</p>
</div>
<div class="col-md-5 pl-md-5">
<h3 class="text-dark"><i class="icon-line-circle-check color mr-1 position-relative" style="top: 2px;color: #1ABC9C;"></i> Our Address</h3>
<p v-if="about">{{about.address}}</p>
</div>
<div class="clear"></div>
</div>
</div>
这是我的 vue 开发工具截图 enter image description here
这是我的回复截图
【问题讨论】:
-
也许您没有得到答案,因为您似乎没有显示正确的代码。您说错误是计算方法未定义。所以告诉我们你在哪里调用这个方法,用什么名字。提示:不要对计算属性使用大写字母。它不符合编码标准,甚至可能是您的问题的原因。
-
我展示了调用计算方法的位置...当我为管理站点调用它时,它运行良好,但对于客户端站点,它在 vue 开发工具中显示未定义
-
不,它没有。在你的 Vue 模板中的某个地方应该有类似:
v-for="item of About"。这就是错误的来源。你没有表现出来。您正在展示执行About计算属性时发生的情况。但你的问题是它甚至没有被执行。 -
好的...我明白了..所以我需要使用 v-for...但我不想循环它...我只需要一行...是这是问题???我必须使用 v-for???...我正在编辑问题并向您展示它在哪里执行
-
好的,谢谢,它有帮助。在您的后端,您正在发送一个数组。在您的前端,您尝试使用键访问该数组,但您的后端没有发送关联数组。你的后端代码应该是:
return response()->json(['about' => $about],200);注意=>。
标签: laravel vue.js vue-component vuex vue-devtools