link2740 link2741 link2742 link2743 link2744 link2745 link2746 link2747 link2748 link2749 link2750 link2751 link2752 link2753 link2754 link2755 link2756 link2757 link2758 link2759 link2760 link2761 link2762 link2763 link2764 link2765 link2766 link2767 link2768 link2769 link2770 link2771 link2772 link2773 link2774 link2775 link2776 link2777 link2778 link2779 link2780 link2781 link2782 link2783 link2784 link2785 link2786 link2787 link2788 link2789 link2790 link2791 link2792 link2793 link2794 link2795 link2796 link2797 link2798 link2799 link2800 link2801 link2802 link2803 link2804 link2805 link2806 link2807 link2808 link2809 link2810 link2811 link2812 link2813 link2814 link2815 link2816 link2817 link2818 link2819 link2820 link2821 link2822 link2823 link2824 link2825 link2826 link2827 link2828 link2829 link2830 link2831 link2832 link2833 link2834 link2835 link2836 link2837 link2838 link2839 link2840 link2841 link2842 link2843 link2844 link2845 link2846 link2847 link2848 link2849 link2850 link2851 link2852 link2853 link2854 link2855 link2856 link2857 link2858 link2859 link2860 link2861 link2862 link2863 link2864 link2865 link2866 link2867 link2868 link2869 link2870 link2871 link2872 link2873 link2874 link2875 link2876

[Vue.js] Vuejs v-for without JSON object

I simply want to loop the numbers from 0 to 19 using a v-for attribute.
I already tried this:
<div v-for=”number in […Array(6).keys()]“ v-bind:key=”number”>
And this:
<div v-for=”let i = 0; i < 20; i++” v-bind:key=”i”>
Of course I could just use an array with all numbers, but I think there is a better way to do this.

Solution :

You could loop through a fixed n number like :

<div v-for=”i in 20” v-bind:key=”i”>

[Vue.js] Why vue CSS priority rules don't work right?

Why css priority doesn’t work right:


<body>
<div class=”class1”>
<div attribute class=”class2”></div>
</div>
</body>

div[attribute] {
color: red;
}

.class1 .class2 {
color: blue;
}

div with class2 will have color: red, but in priority rules color must be blue. This is vue.js feature?

Solution :

Text appears blue to me.

new Vue({
el: “#app”,
template: `
<div class=”class1”>
<div attribute class=”class2”>Text</div>
</div>`
})
div[attribute] {
color: red;
}

.class1 .class2 {
color: blue;
}
<script src=”https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>

<div id=”app”></div>

[Vue.js] Ho to define vue-router components inside a html?

I’m using django + vue.js + vue-router.js to make my project. I’m trying to use vue-router in a single html page. I searched for a while, all examples are use .vue.js components, or define the component templates in js part simpely, just like this:

<body>
<div id=”app”>
<div>
<router-link to=”/foo”>Go to Foo</router-link>
<router-link to=”/bar”>Go to Bar</router-link>
</div>
<div>
<router-view></router-view>
</div>
</div>
<script>
const Foo = { template: ‘<div>foo</div>’ }
const Bar = { template: ‘<div>bar</div>’ }

const routes = [
{ path: ‘/foo’, component: Foo },
{ path: ‘/bar’, component: Bar }
]

const router = new VueRouter({
routes
})

const app = new Vue({
router
}).$mount(‘#app’)
</script>
</body>

What is define the template outside the js part something like this:

<body>
<div id=”app”>
<div>
<router-link to=”/foo”>Go to Foo</router-link>
<router-link to=”/bar”>Go to Bar</router-link>
</div>
<div>
<router-view></router-view>
</div>
</div>

<template id=”Foo”>
<div>this is Foo</div>
</template>
<template id=”Bar”>
<div>this is Bar</div>
</template>

<script>
const Foo = { template: ‘#Foo’ }
const Bar = { template: ‘#Bar’ }

const routes = [
{ path: ‘/foo’, component: Foo },
{ path: ‘/bar’, component: Bar }
]

const router = new VueRouter({
routes
})

const app = new Vue({
router
}).$mount(‘#app’)
</script>
</body>

I tried this, but not work. So how to define vue-router components inside a html? I’m new with vue..

Solution :

You need to add <router-view/> in html file. e.g.

const Foo = { template: ‘<div>this is Foo</div>’ }
const Bar = { template: ‘<div>this is Bar</div>’ }

const routes = [
{ path: ‘/foo’, component: Foo },
{ path: ‘/bar’, component: Bar }
]

const router = new VueRouter({
routes
})

const app = new Vue({
router
}).$mount(‘#app’)
<div id=”app”>
<router-view/>
</div>

[Vue.js] Reference Error document is not defined in nativescript-vue

var elements = document.getElementsByClassName(‘active’);
while(elements.length > 0){
elements[0].classList.remove(‘active’);
}
$event.target.classList.add(‘active’);

It shows document not defined error

Solution :

you’re using web technologies in a native mobile application. The best way to get the id of the element is by giving the element a ref and doing something like this:

const textField = this.$refs.groceryTextField.nativeView

There are lots of good examples in this repo: https://github.com/tralves/groceries-ns-vue/ and you can also checkout the marketplace for more code samples: market.nativescript.org.

[Vue.js] How can I make different HTML and component files in Vue.js?

Is there any way to make two different files for HTML and components in Vue.js? Meaning the HTML code is in a different file from the component’s code like with Angular.

Solution :

If the goal isn’t to avoid precompilation, you can still use single-file components and split the js and css into different files. They include an example of this structure in the docs.

<template>
<div>This will be pre-compiled</div>
</template>
<script src=”./my-component.js”></script>
<style src=”./my-component.css”></style>

[Vue.js] How can i deploy vue cli dist folder in direct admin control panel?

when creating a SPA (single page application) with vue-cli.

I run npm run build script then upload dist files in the public_html folder but didn’t work

My hosting service uses direct admin as a web control panel.

I read the deployment guide but didn’t help me

hosting service that I use works with Linux, PHP, and Apache

Solution :

By default, vue.js CLI assumes the app will be deployed at the root of a domain, e.g. https://www.my-app.com/. If the app is deployed at a sub-path, you will need to specify that sub-path using this option. For example, if the app is deployed at https://www.foobar.com/my-app/, set publicPath to ‘/my-app/‘.

Read public_path guide

[Vue.js] Conditional nav in app.vue depending on user logged in state from main.js

to show a different navigation in app.vue.js depending on user is signed
in or not. i came complicated solutions, isn’t there a simple if else solution or pass function from main.js to vue.js? thanks in advance

#main.js
Vue.loggedIn=true

new Vue({
router,
render: h => h(App)
}).$mount(‘#app’)

#App.vue

<template>
<div id=”app”>
<div v-if=”Vue.loggedIn” id=”nav”>
<router-link to=”/about”>About</router-link> |
<router-link to=”/register”>Register</router-link> |
<router-link to=”/login”>Login</router-link> |
<router-link to=”/profile”>profile</router-link>
</div>
<div v-else id=”nav”>

..
</div>
<router-view/>
</div>
</template>

Solution :

Generally, App should use its own data items, so if you define loggedIn as one, you should get the behavior you want. Since it’s the top-level module, I don’t expect you want to pass it in as a prop, although that would work, too.

const App = {
data() {
return {
loggedIn: false
};
},
template: ‘#app-template’,
mounted() {
setTimeout(() => this.loggedIn = true, 2500);
}
};

new Vue({
render(h) {
return h(App);
}
}).$mount(‘#app’)
<script src=”https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id=”app”>
</div>

<template id=”app-template”>
<div id=”nav”>
<template v-if=”loggedIn”>
This is the logged in one
</template>
<template v-else>
This is the not logged in one.
</template>
</div>
</template>

[Vue.js] How to clean input value after submitting it in Vuejs?

when doing a to-do list in Vuejs. When I enter a new value in the input field, it seems that I can’t make the input value to reset. How can I accomplish this?

there is tried to grab the input value & reset it to an empty string, but I haven’t had any luck.

HTML Code:

<div id=”app”>
<h1>{ message }</h1>
<form v-on:submit=”addNewTodo”>
<input class=”input-value” v-model=”todo.task” type=”text”>
<button type=”submit”>Add todo</button>
</form>
<ul>
<li v-for=”todo in todos” :class=”{ completed: todo.isActive }” @click=”$set(todo, ‘isActive’, !todo.isActive)”>
{ todo.task } <span v-on:click=”deleteTodo”>{ todo.delete }</span>
</li>
</ul>
</div>

JS Code:

var app = new Vue({
el: ‘#app’,
data: {
message: ‘List of things to do today’,
todos: [
{ task: ‘Have breakfast’, delete:’(x)’},
{ task: ‘Go to the gym’, delete:’(x)’},
{ task: ‘Study Vuejs’, delete:’(x)’}
],
todo: {task: ‘’, delete: ‘(x)’}
},
methods: {
addNewTodo: function(e){

e.preventDefault();
this.todos.push( this.todo );

var inputValue = document.querySelectorAll(‘.input-value’).value;

inputValue = ‘’
},
deleteTodo: function(){
this.todos.shift( this.todo )
}
}
});

Solution :

Try to flush it like :

addNewTodo: function(e){

e.preventDefault();
this.todos.push( this.todo );
this.todo.task=””
}

Solution 2:

the input value is two-way binded with todo.task so you could do the following after adding a new todo task.

this.todo.task = ‘’

[Vue.js] Vue sibling components hooks lifecycle relationships

Never used Vue.js before. there is one parent component and 2 child components. These 2 child components should interact with asynchronous actions using the native vue.js event bus system (with a dummy vue.js object used as a shared container for the event bus).

Having something like the following:

EventBus.js

import vue.js from “vue”
export default new Vue()

Parent.vue

import Child1 from “./Child1.vue”
import Child2 from “./Child2.vue”

export default {

name: “Parent”,

components: {
child1: Child1,
child2: Child2,
}

}

Child1.vue

import EventBus from “./EventBus”

export default {

name: “Child1”,

beforeCreate () {

EventBus.$once(“MY_EVENT_X”, async () => {
EventBus.$emit(“MY_EVENT_Y”)
})

},

mounted () {
// something
}

}

Child2.vue

import EventBus from “./EventBus”

export default {

name: “Child2”,

beforeCreate () {

EventBus.$once(“MY_EVENT_Y”, async () => {
// do something
})

},

mounted () {
EventBus.$emit(“MY_EVENT_X”)
}

}

My question: having the events handlers defined in the “beforeCreate” hook, can I be sure that the “beforeCreate” hooks of both Child1 and Child2 components will be initiliazed before any of the “mounted” hooks of Child1 or Child2 will be called by Vue?

Solution :

You can leverage component hook order between parent and children. When parent mounted is called, we will be sure all child components is created and mounted.

Image source from here

To do it, you can define a boolean flag in parent, change this flag to true in mounted hook:

import Child1 from “./Child1.vue”
import Child2 from “./Child2.vue”

export default {

name: “Parent”,

components: {
child1: Child1,
child2: Child2,
},
data: () => ({
isChildMounted: false
}),
mounted() {
this.isChildMounted = true
}
}

Make sure to pass this flag to children component:

<child-2 :isReady=”isChildMounted” />

Finally, in child component, watching for props change. When the flag is changed to true, we know all child components is ready. It’s time to emit event.

import EventBus from “./EventBus”

export default {
name: “Child2”,
props: [‘isReady’],
beforeCreate () {
EventBus.$once(“MY_EVENT_Y”, async () => {
// do something
})
},
watch: {
isReady: function (newVal) {
if (newVal) {
// all child component is ready
EventBus.$emit(“MY_EVENT_X”)
}
}
}
}

[Vue.js] How to add a outside layout to vue component

I wanted to change my vue.js route from /dashboards/dashboard to just /dashboard. How to achieve these using this code

import Layout2 from ‘@/layout/Layout2’

export default [
{
path: ‘/dashboards’,
component: Layout2,
children: [
{
path: ‘dashboard’,
name: ‘dashboard’,
component: () => import(‘@/components/dashboards/Dashboard’)
}
]
}
]

there is tried putting these code but how can I add the layout2 if the component is already added?

export default [
{
path: ‘/dashboard’,
name: ‘dashboard’,
component: () => import(‘@/components/dashboards/Dashboard’)
}
]

Solution :

If vue-router document

Note that nested paths that start with / will be treated as a root
path. This allows you to leverage the component nesting without having
to use a nested URL.

You can do

import Layout2 from ‘@/layout/Layout2’

export default [
{
path: ‘/‘,
component: Layout2,
children: [
{
path: ‘dashboard’,
name: ‘dashboard’,
component: () => import(‘@/components/dashboards/Dashboard’)
}
]
}
]