Created
March 28, 2019 14:01
-
-
Save ManzDev/2a865a197e0e4e53e5108354df83dfb2 to your computer and use it in GitHub Desktop.
VueRouter & VueI18n example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "title": "¡Hola!", | |
| "text": "Esto es un ejemplo" | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!DOCTYPE html> | |
| <html lang="en"> | |
| <head> | |
| <meta charset="UTF-8"> | |
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
| <meta http-equiv="X-UA-Compatible" content="ie=edge"> | |
| <script defer src="./index.js"></script> | |
| <title>Document</title> | |
| </head> | |
| <body> | |
| <div id="app"> | |
| <p> | |
| <router-link to="/es">Spanish</router-link> | |
| <router-link to="/en">English</router-link> | |
| </p> | |
| <router-view></router-view> | |
| </div> | |
| </body> | |
| </html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Vue from 'vue/dist/vue.esm'; | |
| import VueI18n from 'vue-i18n'; | |
| import VueRouter from 'vue-router'; | |
| Vue.use(VueI18n); | |
| Vue.use(VueRouter); | |
| import messages from './lang/*.json'; | |
| const Comp = { | |
| props: ['lang'], | |
| template: `<div> | |
| <h2>{{ $t('title') }}</h2> | |
| <p>{{ $t('text') }}</p> | |
| </div>`, | |
| watch: { | |
| lang: { | |
| handler(current, old) { | |
| i18n.locale = current; | |
| }, | |
| immediate: true | |
| } | |
| } | |
| } | |
| const routes = [ | |
| { path: '/es', component: Comp, props: { lang: 'es' } }, | |
| { path: '/en', component: Comp, props: { lang: 'en' } } | |
| ]; | |
| const i18n = new VueI18n({ | |
| locale: 'es', | |
| fallbackLocale: 'en', | |
| messages | |
| }); | |
| const app = new Vue({ | |
| el: '#app', | |
| router: new VueRouter({ routes }), | |
| mounted() { | |
| routes.push({ path: '/fr', component: Comp, params: { lang: 'fr' } }); | |
| }, | |
| i18n | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment