Nativescript Vue:访问 class 个函数
Nativescript Vue: access class functions
我目前正在使用 Nativescript 和 Vuejs 构建应用程序。
我使用 Material BottomNavigationBar (https://github.com/Akylas/nativescript-material-components/blob/master/packages/nativescript-material-bottomnavigationbar/README.md).
我需要使用两种方法:
- selectTab(索引:数字)
- showBadge(index: number, value?: number)
现在我需要调用这些方法,但出现了问题。我怎么做?
我的代码:
main.js
import BottomNavigationBar from 'nativescript-material-bottomnavigationbar/vue';
import BottomNavigationTab from 'nativescript-material-bottomnavigationbar/vue';
Vue.use(BottomNavigationBar);
Vue.use(BottomNavigationTab);
Footer.vue:
<BottomNavigationBar titleVisibility="Never" activeColor="#0285ff" inactiveColor="#5c687c"
backgroundColor="#f5f5f5" @tabSelected="onBottomNavigationTabSelected" row="1"
class="footer" ref="navBar" :selectedTab="2">
<BottomNavigationTab icon="~/assets/images/logo.png"/>
<BottomNavigationTab icon="~/assets/images/chat.png"/>
<BottomNavigationTab icon="~/assets/images/settings.png"/>
</BottomNavigationBar>
...
mounted() {
this.$refs.navBar.nativeView.selectTab(2)
}
这不起作用,并表示 nativeView 未定义。
有没有办法访问这些 class 方法?
此致,
托拜厄斯
您可以在标签中放置一个 ref="mybar",然后在您的组件脚本中的 this.$refs.mybar
下找到它。
就像在 this example 上所做的一样。
有关详细信息,请参阅 vue documentation。
您的组件在触发挂载时尚未加载。
我建议在您的导航栏组件上使用加载事件,然后触发您的方法。
<BottomNavigationBar titleVisibility="Never" activeColor="#0285ff" inactiveColor="#5c687c"
backgroundColor="#f5f5f5" @tabSelected="onBottomNavigationTabSelected" row="1" @loaded="onNavbarLoaded"
class="footer" ref="navBar" :selectedTab="2">
<BottomNavigationTab icon="~/assets/images/logo.png"/>
<BottomNavigationTab icon="~/assets/images/chat.png"/>
<BottomNavigationTab icon="~/assets/images/settings.png"/>
</BottomNavigationBar>
onNavbarLoaded(evt) {
this.$refs.navBar.nativeView.selectTab(2);
// I am not sure if you actually have to easy nativeView..
this.$refs.navBar.selectTab(2);
}
找到解决方案!
需要等待组件加载完成。
我现在的方式:
组件:
<BottomNavigationBar titleVisibility="Never" activeColor="#0285ff" inactiveColor="#5c687c"
backgroundColor="#f5f5f5" @tabPressed="pressedNavigation" @tabSelected="pressedNavigation"
row="1" class="footer" ref="navBar" @loaded="loaded">
<BottomNavigationTab icon="~/assets/images/logo.png"/>
<BottomNavigationTab icon="~/assets/images/chat.png"/>
<BottomNavigationTab icon="~/assets/images/settings.png"/>
</BottomNavigationBar>
方法:
loaded(args) {
this.loadedNavBar = true;
this.navBar = args.object
if (this.selectedTab !== null) this.navBar.selectTab(this.selectedTab)
},
I select 索引并将其存储在变量中。加载组件后,我可以调整 selection.
我目前正在使用 Nativescript 和 Vuejs 构建应用程序。
我使用 Material BottomNavigationBar (https://github.com/Akylas/nativescript-material-components/blob/master/packages/nativescript-material-bottomnavigationbar/README.md).
我需要使用两种方法:
- selectTab(索引:数字)
- showBadge(index: number, value?: number)
现在我需要调用这些方法,但出现了问题。我怎么做?
我的代码:
main.js
import BottomNavigationBar from 'nativescript-material-bottomnavigationbar/vue';
import BottomNavigationTab from 'nativescript-material-bottomnavigationbar/vue';
Vue.use(BottomNavigationBar);
Vue.use(BottomNavigationTab);
Footer.vue:
<BottomNavigationBar titleVisibility="Never" activeColor="#0285ff" inactiveColor="#5c687c"
backgroundColor="#f5f5f5" @tabSelected="onBottomNavigationTabSelected" row="1"
class="footer" ref="navBar" :selectedTab="2">
<BottomNavigationTab icon="~/assets/images/logo.png"/>
<BottomNavigationTab icon="~/assets/images/chat.png"/>
<BottomNavigationTab icon="~/assets/images/settings.png"/>
</BottomNavigationBar>
...
mounted() {
this.$refs.navBar.nativeView.selectTab(2)
}
这不起作用,并表示 nativeView 未定义。
有没有办法访问这些 class 方法?
此致,
托拜厄斯
您可以在标签中放置一个 ref="mybar",然后在您的组件脚本中的 this.$refs.mybar
下找到它。
就像在 this example 上所做的一样。
有关详细信息,请参阅 vue documentation。
您的组件在触发挂载时尚未加载。 我建议在您的导航栏组件上使用加载事件,然后触发您的方法。
<BottomNavigationBar titleVisibility="Never" activeColor="#0285ff" inactiveColor="#5c687c"
backgroundColor="#f5f5f5" @tabSelected="onBottomNavigationTabSelected" row="1" @loaded="onNavbarLoaded"
class="footer" ref="navBar" :selectedTab="2">
<BottomNavigationTab icon="~/assets/images/logo.png"/>
<BottomNavigationTab icon="~/assets/images/chat.png"/>
<BottomNavigationTab icon="~/assets/images/settings.png"/>
</BottomNavigationBar>
onNavbarLoaded(evt) {
this.$refs.navBar.nativeView.selectTab(2);
// I am not sure if you actually have to easy nativeView..
this.$refs.navBar.selectTab(2);
}
找到解决方案!
需要等待组件加载完成。
我现在的方式:
组件:
<BottomNavigationBar titleVisibility="Never" activeColor="#0285ff" inactiveColor="#5c687c"
backgroundColor="#f5f5f5" @tabPressed="pressedNavigation" @tabSelected="pressedNavigation"
row="1" class="footer" ref="navBar" @loaded="loaded">
<BottomNavigationTab icon="~/assets/images/logo.png"/>
<BottomNavigationTab icon="~/assets/images/chat.png"/>
<BottomNavigationTab icon="~/assets/images/settings.png"/>
</BottomNavigationBar>
方法:
loaded(args) {
this.loadedNavBar = true;
this.navBar = args.object
if (this.selectedTab !== null) this.navBar.selectTab(this.selectedTab)
},
I select 索引并将其存储在变量中。加载组件后,我可以调整 selection.