43
loading...
This website collects cookies to deliver better user experience
v-model
for two-way data binding. This means we can bind the value of an input
element to a property of our component’s data using the v-model
.v-if
for conditional rendering, v-model
for two-way data binding, v-for
for list rendering, and Vuex for state management.$ npm install --g vue-native-cli
$ npm install --g expo-cli
vue-native init <yourProjectName>
$ cd <yourProjectName>
$ npm start
http://localhost:19002/
. By visiting http://localhost:8080/
in a web browser, the following page appears:view
component works just like the div
tag in our regular HTML. This component is the fundamental building block for creating user interfaces in Vue Native, just like in React Native.view
component, like in the following code:<template>
<view class="container">
<text>My Awesome Vue Native App</text>
</view>
</template>
h1
or p
. Instead, we must use the <text>…</text>
component. Using this component is pretty straightforward:<template>
<text>Hello World</text>
</template>
image
component renders static images, network images, and images from a user’s device.img
tag where we use the src
attribute, here we bind the source
attribute in our image
component to load our images dynamically. This allows webpack to bundle our image assets during the build process.<template>
<!-- Network image -->
<image
:style="{ width: 300, height: 150 }"
:source="{
uri:
'https://images.unsplash.com/photo-1621570074981-ee6a0145c8b5?ixid=MnwxMjA3fDF8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=750&q=80',
}"
/>
<!-- Static image -->
<image
:style="{ width: 300, height: 150 }"
:source="require('./assets/photo.jpg')"
/>
<!-- Local disk image -->
<image
:style="{width: 66, height: 58}"
:source="{uri: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADMAAAAzCAYAAAA6oTAqAAAAEXRFWHRTb2Z0d2FyZQBwbmdjcnVzaEB1SfMAAABQSURBVGje7dSxCQBACARB+2/ab8BEeQNhFi6WSYzYLYudDQYGBgYGBgYGBgYGBgYGBgZmcvDqYGBgmhivGQYGBgYGBgYGBgYGBgYGBgbmQw+P/eMrC5UTVAAAAABJRU5ErkJggg=='}"
/>
</template>
TextInput
component inputs text into the app through the user’s keyboard. We can use v-model
to bind data in our state to the TextInput
component. This allows us to seamlessly get and set the value of TextInput
:<template>
<view class="container">
<text-input
:style="{
height: 30,
width: 250,
borderColor: '#511281',
borderWidth: 1,
}"
v-model="text"
/>
</view>
</template>
<script>
export default {
data() {
return {
text: "",
};
},
};
</script>
v-model
. We can explore two-way data binding with the v-model
directive, like below:<view class="container">
<text-input
:style="{
height: 30,
width: 250,
borderColor: '#511281',
borderWidth: 1,
}"
v-model="text"
/>
</view>
</template>
<script>
export default {
data() {
return {
text: "",
};
},
};
</script>
npm i vue-native-router
project
directory root to install these packages:npm i react-native-reanimated react-native-gesture-handler react-native-paper
StackNavigator
and DrawerNavigator
to register screens for navigation:<script>
import {
createAppContainer,
createStackNavigator,
} from "vue-native-router";
import SettingsScreen from "./screens/SettingsScreen.vue";
import HomeScreen from "./screens/HomeScreen.vue";
const StackNavigator = createStackNavigator(
{
Settings: SettingsScreen,
Home: HomeScreen,
},
{
initialRouteName: 'Home',
}
);
const AppNavigator = createAppContainer(StackNavigator);
export default {
components: { AppNavigator },
}
</script>
navigate
method on the navigation
object, which is passed down as a prop like this:<script>
export default {
// navigation is declared as a prop
props: {
navigation: {
type: Object
}
},
methods: {
navigateToScreen() {
this.navigation.navigate("Profile");
}
}
}
</script>
npm i vuex
//or
yarn add vuex
state
, getters
, mutations
, or actions
depending on the application‘s needs. To keep things simple, use the state
object here:// store/index.js
import Vue from 'vue-native-core';
import Vuex from 'vuex';
Vue.use(Vuex);
const store = new Vuex.Store({
state: {
name: 'Ejiro Asiuwhu',
},
});
export default store;
<script>
import store from "./store";
export default {
computed: {
name() {
return store.state.name;
},
},
};
</script>
this.$store
as we would typically do in a Vue and Vuex app setup.<template>
<view class="container">
<button
:on-press="getLocation"
title="Get Location"
color="#184d47"
accessibility-label="Get access to users' location"
>
<text>Location Details:</text>
<text>{{ location }}</text>
<text>Latitude: {{ latitude }}</text>
<text>Longitude: {{ longitude }}</text>
<text class="text-error">{{ errorMessage }}</text>
</view>
</template>
<script>
import * as Location from "expo-location";
export default {
data() {
return {
location: "",
latitude: "",
longitude: "",
errorMessage: "",
text: "",
user: {
country: "",
},
};
},
methods: {
async getLocation() {
try {
let { status } = await Location.requestForegroundPermissionsAsync();
if (status !== "granted") {
this.errorMessage = "Permission to access location was denied";
return;
}
let location = await Location.getCurrentPositionAsync({});
this.location = location;
this.latitude = location.coords.latitude;
this.longitude = location.coords.longitude;
this.errorMessage = "";
} catch (error) {
this.errorMessage = error;
}
},
},
}
</script>
.vue
components and integrate Expo and React Native packages into apps with little to no extra configurations.