【发布时间】:2019-06-29 20:36:02
【问题描述】:
编辑:请参阅下面关于向 Vue 模板添加元素的评论
今天再次编辑:好的,这实际上是导致 this.$navigateTo 停止工作的原因...如果我导入一个服务以对 Friends.vue 中的休息 api 进行 http 调用要在mounted() 事件中获取数据,这会导致this.$navigateTo 从调用Home.vue 停止工作。谁能提出原因?
这是在 Friends.vue...
mounted() {
console.log(`${this.codeFile}: mounted() fired`);
httpService.getAllFriends((data, err) => {
this.friends = data;
if (err) {
console.error(
`${this.codeFile}: mounted: getAllFriends: `,
err
);
}
});
},
我真的觉得我的代码看起来像文档,但我的 this.$navigateTo(Friends) 什么也没做。我可以看到console.log 消息很好,但页面没有改变。我在文档中没有看到什么?
<template>
<Page class="page">
<ActionBar class="action-bar" title="Home"></ActionBar>
<ScrollView>
<StackLayout backgroundColor="#3c495e">
<Label
text="Friends"
@tap="gotoFriendsPage()"
height="70"
backgroundColor="#43b883"
/>
<Label text="Places" height="70" backgroundColor="#289062"/>
<Label text="Messages" height="70" backgroundColor="#1c6b48"/>
</StackLayout>
</ScrollView>
</Page>
</template>
<script>
import Friends from "./Friends";
export default {
data() {
return {
codeFile: "Home.vue",
friendsPage: Friends
};
},
created() {
//console.log(`${this.codeFile}: created() fired`);
},
mounted() {
//console.log(`${this.codeFile}: mounted() fired`);
},
computed: {},
methods: {
gotoFriendsPage: function() {
console.log(`${this.codeFile}: gotoFriendsPage fired`);
this.$navigateTo(Friends);
}
}
};
</script>
<style scoped lang="scss">
// Start custom common variables
@import "../_app-variables.scss";
// End custom common variables
// Custom styles
.action-bar {
background-color: $blue-dark;
color: $blue-light;
}
.body {
color: gray;
}
.fa {
color: $accent-dark;
font-size: 16;
}
.col-text {
text-align: left;
margin-bottom: 3;
padding: 5;
}
.col-title {
text-align: left;
margin-bottom: 0;
padding: 5;
padding-bottom: 0;
font-weight: bold;
}
.info {
font-size: 20;
}
</style>
编辑:添加 app.js
import Vue from "nativescript-vue";
import Home from "./components/Home";
const template = `
<Frame>
<Home />
</Frame>
`;
new Vue({
template: template,
components: {
Home
},
mounted: function () {
//console.log('app.js mounted fired...');
},
created: function () {
//console.log('app.js created fired...');
}
}).$start();
【问题讨论】:
-
可能问题出在设备或编译中。我在这里看不出有什么奇怪的。根据您的代码,我在这里创建了示例 play.nativescript.org/?template=play-vue&id=vmbEXA 并且在使用 Android 上的 Playground 应用程序检查时它适用于我
-
嗯,你是对的,这在我的手机上运行得非常好。到目前为止,我只在模拟器上进行测试,无法让它工作。我将在今天晚些时候更详细地处理这个问题。谢谢@jakob!
-
@jakob 是的,这段代码在本地工作。那么我做错了什么?是我在 app.js 中实例化 Vue 的方式吗?请参阅下面的回复。我是根据演示/示例代码完成的,这与您发布的示例略有不同。
-
@jakob 和后来发现的任何人,jakobs 简单示例确实确实在操场和本地工作。当向目标 Friends.vue 模板添加更多元素时,OP 中的问题
this.$navigateTo什么都不做。如果您将其他 StackLayout 或 GridLayout 或 ListView 或 anything (似乎)添加到目标,the this.$navigateToSTOPS 工作。为什么? -
你能在 nativescript play 上创建和你一样的例子吗?
标签: navigation nativescript-vue