有兴趣了解更多关于 NativeScript 和 Vue 的信息吗?观看 YouTube 上的 NativeScript-Vue 2.0 网络研讨会
“具有 AOT 编译和 tree-shaking 的最新版本的 Angular 已能够大幅减小其大小。但是,包含 Vuex + vue-router 的完整功能的 Vue 2 项目(约 30kb 压缩后)仍然明显轻于 angular-cli 生成的开箱即用、经过 AOT 编译的应用程序(约 130kb 压缩后)。” 来源
请注意,目前该项目在 Android 模拟器上的运行更加稳定,而不是 iOS 模拟器。
{
"main": "app-with-list-view.js",
"name": "nativescript-template-tutorial",
"version": "1.0.1"
}
成功使用 @vuejs 实例来控制 @NativeScript 原生标签!无法表达我对一个标签的喜悦 😂 pic.twitter.com/BQUdxNEfEJ
— Igor Randjelovic (@igor_randj) 2017 年 4 月 19 日
const Vue = require('nativescript-vue/dist/index')
new Vue({
data: {
test: 'testing',
test2: 50,
test3: 30
},
template: `
<stack-layout>
<button @tap="onTap">whatever
<text-field v-model="test"></text-field>
<slider v-model.number="test2">
<slider v-model.number="test3" minvalue="-10" maxvalue="50" style="margin-top: 15;">
</stack-layout>
`,
methods: {
onTap() {
this.test = 'changed'
this.test2 = 42
}
}
}).$start()
data: {
subreddit: '/r/funny'
},
methods: {
onTap() {
this.test = 'changed'
this.test2 = 42
}
}
const Vue = require('nativescript-vue/dist/index')
const VueRouter = require('vue-router')
Vue.use(VueRouter)
global.process = {env: {}} // hack! a build process should replace process.env's with static strings.
const http = require("http")
const SegmentedBarItem = require('tns-core-modules/ui/segmented-bar').SegmentedBarItem
Vue.prototype.$http = http
const Recipe = {
data: function(){
return {
recipe: []
}
},
created() {
var id = this.$route.params.id
this.fetchOneRecipe(id)
var firstItem = new SegmentedBarItem();
var secondItem = new SegmentedBarItem();
var thirdItem = new SegmentedBarItem();
firstItem.title = "Ingredients";
secondItem.title = "Tools";
thirdItem.title = "Procedure";
this.recipeSteps = [ firstItem, secondItem, thirdItem ];
},
template: `
<stack-layout>
<img :src="recipe.image" height="25%" stretch="aspectFill">
<stack-layout class="innerCard">
<segmented-bar class="bar" bordercolor="#8AC215" :items="recipeSteps" selectedbackgroundcolor="#8AC215" #sb="" selectedindex="0" @selectedindexchange="changeTab(sb)"></segmented-bar>
<stack-layout verticalalignment="top">
<scroll-view height="75%" verticalalignment="top">
</scroll-view>
<stack-layout height="25%" verticalalignment="center">
</stack-layout>
</stack-layout>
</stack-layout>
</stack-layout>
`,
methods: {
fetchOneRecipe(id){
this.$http.getJSON(`https://quicknoms-91e39.firebaseio.com/Recipes.json?orderBy="$key"&equalTo="${id}"`).then((res) => {
this.recipe = res;
for( var key in res) {
this.recipe.name = res[key].Name
this.recipe.image = res[key].Image
this.recipe.notes = res[key].Notes
this.recipe.procedure = res[key].Method
}
console.log(JSON.stringify(this.recipe))
}).catch((err) => {
console.log('err..' + err)
})
},
changeTab(id){
switch (id) {
case 0:
this.procedure = this.ingredients;
break;
case 1:
this.procedure = this.tools;
break;
case 2:
this.procedure = this.method;
break;
}
}
}
}
const Recipes = {
data: function(){
return {
recipes: []
}
},
created() {
this.fetchRecipes()
},
template: `
<scroll-view class="green">
<wrap-layout horizontalalignment="center">
<stack-layout style="margin-left: 10" class="card" width="45%" v-for="(recipe, i) in recipes" key="i">
<stack-layout horizontalalignment="center" @tap="$router.push({ name:'recipe',params: {id: recipe.id} })">
<img :src="recipe.image">
</stack-layout>
</stack-layout>
</wrap-layout>
</scroll-view>
`
,
methods: {
fetchRecipes() {
this.$http.getJSON(`https://quicknoms-91e39.firebaseio.com/Recipes.json`).then((res) => {
for( var key in res) {
this.recipes.unshift({id : key, name: res[key].Name, image: res[key].Image})
}
}).catch((err) => {
console.log('err..' + err)
})
}
}
}
const router = new VueRouter({
routes: [
{path: '/recipes', component: Recipes},
{path: '/recipe/:id', name: 'recipe', component: Recipe},
{path: '*', redirect: '/recipes'}
]
})
router.replace('/recipes')
new Vue({
router,
template: `
<stack-layout>
<router-view></router-view>
</stack-layout>
`
}).$start()