I am using vue-multiselect to display a multi select dropdown list on UI in vue js.
It is present in component section of vue extension by browser, CSS is also present in inspect element, but multiselect dropdown is not getting rendered on the UI.
Below is my html and js code, and there is no error in console.
HTML code
<multiselect
v-model="pincodeone"
tag-placeholder="Add this as new tag"
tag-position="bottom"
placeholder="Enter the pincode(s)"
label="name"
:options="options"
:multiple="true"
:taggable="true"
#tag="PincodeTag"
#remove="RemoveTag"
></multiselect>
Js code
import Multiselect from 'vue-multiselect';
import 'vue-multiselect/dist/vue-multiselect.min.css';
export default {
name: "test",
components: { Multiselect },
props: [],
data() {
return { options: [], pincodeone: [], pincodetwo: [] };
},
methods: {
PincodeTag(newTag) {
const tag = { name: newTag };
this.options.push(tag);
this.pincodeone.push(tag);
this.pincodetwo.push(newTag);
},
RemoveTag({ name }) {
const index = this.pincodetwo.indexOf(name);
if (index > -1) {
this.pincodetwo.splice(index, 1);
this.options.splice(index, 1);
}
},
}; }}}}
package.json file
"vue-multiselect": "^2.1.6",
This is the error I get in console running your code
[Vue warn]: Property or method "RemoveTag" is not defined on the instance but referenced during render.
The problem is that your RemoveTag method is outside of the methods object. Your entire methods object should be like this:
methods: {
PincodeTag(newTag) {
const tag = { name: newTag };
this.options.push(tag);
this.pincodeone.push(tag);
this.pincodetwo.push(newTag);
},
RemoveTag({ name }) {
const index = this.pincodetwo.indexOf(name);
if (index > -1) {
this.pincodetwo.splice(index, 1);
}
}
}
Related
datalist.js
import axios from "axios";
export const datalist = () => {
return axios.get("myapiurl/name...").then((response) => response);
};
HelloWorld.vue
<template>
<div>
<div v-for="item in items" :key="item.DttID">
<router-link
:to="{
name: 'UserWithID',
params: { id: item.DepaD },
query: { DepaD: item.DepaID },
}"
>
<div class="bt-color">{{ item.DepaName }}</div>
</router-link>
</div>
<br /><br /><br />
<User />
</div>
</template>
<script>
import User from "./User.vue";
import { datalist } from "./datalist";
export default {
name: "HelloWorld",
components: {
User,
},
data() {
return {
items: datalist,
};
},
mounted() {
datalist().then((r) => {
this.items = r.data;
});
},
};
</script>
User.vue
<template>
<div>
<div v-for="(item, key) in user" :key="key">
{{ item.Accv }}
</div>
</div>
</template>
<script>
import { datalist } from "./datalist";
export default {
name: "User",
data() {
return {
lists: datalist,
};
},
computed: {
user: function () {
return this.lists.filter((item) => {
if (item.DepaD === this.$route.params.id) {
return item;
}
});
},
},
};
</script>
Error with the code is,
[Vue warn]: Error in render: "TypeError: this.lists.filter is not a function"
TypeError: this.lists.filter is not a function
The above error i am getting in User.vue component in the line number '20'
From the api which is in, datalist.js file, i think i am not fetching data correctly. or in the list filter there is problem in User.vue?
Try to change the following
HelloWorld.vue
data() {
return {
items: [],
};
},
mounted() {
datalist().then((r) => {
this.items = r.data;
});
},
User.vue
data() {
return {
lists: []
};
},
mounted() {
datalist().then((r) => {
this.lists = r.data;
});
},
At least this suppress the error, but i cant tell more based on your snippet since there are network issues :)
Since your datalist function returns a Promise, you need to wait for it to complete. To do this, simply modify your component code as follows:
import { datalist } from "./datalist";
export default {
name: "User",
data() {
return {
// empty array on initialization
lists: [],
};
},
computed: {
user: function() {
return this.lists.filter((item) => {
if (item.DeploymentID === this.$route.params.id) {
return item;
}
});
},
},
// asynchronous function - because internally we are waiting for datalist() to complete
async-mounted() {
this.users = await datalist() // or datalist().then(res => this.users = res) - then async is not needed
}
};
now there will be no errors when initializing the component, since initially lists is an empty array but after executing the request it will turn into what you need.
You may define any functions and import them, but they wont affect until you call them, in this case we have datalist function imported in both HelloWorld and User component, but it did not been called in User component. so your code:
data() {
return {
lists: datalist,
};
},
cause lists to be equal to datalist that is a function, no an array! where .filter() should be used after an array, not a function! that is the reason of error.
thus you should call function datalist and put it's response in lists instead of putting datalist itself in lists
Extra:
it is better to call axios inside the component, in mounted, created or ...
it is not good idea to call an axios command twice, can call it in HelloWorl component and pass it to User component via props
I need a Vue component to show some HTML content in v-data-table from Vuetify. I have seen this post Vue 2 contentEditable with v-model, and I created a similar code shown below.
My problem is the component is not reactive. When I click the "Test button", no content is updated in HtmlTextArea.
<template>
<div>
<v-btn #click="doTest()">Test Button</v-btn>
<HtmlTextArea
v-model="content"
style="max-height:50px;overflow-y: scroll;"
></HtmlTextArea>
</div>
<template>
export default {
name: "ModelosAtestados",
components: { HtmlTextArea },
data: () => ({
content: "",
}),
methods: {
doTest() {
this.content = "kjsadlkjkasfdkjdsjkl";
},
},
};
//component
<template>
<div ref="editable" contenteditable="false" v-on="listeners"></div>
</template>
<script>
export default {
name: "HtmlTextArea",
props: {
value: {
type: String,
default: "",
},
},
computed: {
listeners() {
return { ...this.$listeners, input: this.onInput };
},
},
mounted() {
this.$refs.editable.innerHTML = this.value;
},
methods: {
onInput(e) {
this.$emit("input", e.target.innerHTML);
},
},
};
</script>
This occurs because HtmlTextArea sets the div contents to its value prop only in the mounted lifecycle hook, which is not reactive.
The fix is to setup a watcher on value, so that the div contents are updated to match whenever a change occurs:
// HtmlTextArea.vue
export default {
watch: {
value: {
handler(value) {
this.$refs.editable.innerHTML = value;
}
}
}
}
demo
In the #click event binder, you have to pass a function. You passed the result of an executed function.
To make it work: #click="doTest" or #click="() => doTest()".
How to debug such problems:
Display the value you want to update on your template to check if its updated: {{content}}
Use the vue devtool extension to check the current state of your components
Background: I've built a standard single file component that takes a name prop and looks in different places my app's directory structure and provides the first matched component with that name. It was created to allow for "child theming" in my Vue.js CMS, called Resto. It's a similar principle to how WordPress looks for template files, first by checking the Child theme location, then reverting to the parent them if not found, etc.
Usage : The component can be used like this:
<!-- Find the PageHeader component
in the current child theme, parent theme,
or base components folder --->
<theme-component name="PageHeader">
<h1>Maybe I'm a slot for the page title!</h1>
</theme-component>
My goal : I want to convert to a functional component so it doesn't affect my app's render performance or show up in the Vue devtools. It looks like this:
<template>
<component
:is="dynamicComponent"
v-if="dynamicComponent"
v-bind="{ ...$attrs, ...$props }"
v-on="$listeners"
#hook:mounted="$emit('mounted')"
>
<slot />
</component>
</template>
<script>
import { mapGetters } from 'vuex'
export default {
name: 'ThemeComponent',
props: {
name: {
type: String,
required: true,
default: '',
},
},
data() {
return {
dynamicComponent: null,
resolvedPath: '',
}
},
computed: {
...mapGetters('site', ['getThemeName']),
customThemeLoader() {
if (!this.name.length) {
return null
}
// console.log(`Trying custom theme component for ${this.customThemePath}`)
return () => import(`#themes/${this.customThemePath}`)
},
defaultThemeLoader() {
if (!this.name.length) {
return null
}
// console.log(`Trying default component for ${this.name}`)
return () => import(`#restoBaseTheme/${this.componentPath}`)
},
baseComponentLoader() {
if (!this.name.length) {
return null
}
// console.log(`Trying base component for ${this.name}`)
return () => import(`#components/Base/${this.name}`)
},
componentPath() {
return `components/${this.name}`
}, // componentPath
customThemePath() {
return `${this.getThemeName}/${this.componentPath}`
}, // customThemePath()
},
mounted() {
this.customThemeLoader()
.then(() => {
// If found in the current custom Theme dir, load from there
this.dynamicComponent = () => this.customThemeLoader()
this.resolvedPath = `#themes/${this.customThemePath}`
})
.catch(() => {
this.defaultThemeLoader()
.then(() => {
// If found in the default Theme dir, load from there
this.dynamicComponent = () => this.defaultThemeLoader()
this.resolvedPath = `#restoBaseTheme/${this.defaultThemePath}`
})
.catch(() => {
this.baseComponentLoader()
.then(() => {
// Finally, if it can't be found, try the Base folder
this.dynamicComponent = () => this.baseComponentLoader()
this.resolvedPath = `#components/Base/${this.name}`
})
.catch(() => {
// If found in the /components dir, load from there
this.dynamicComponent = () => import(`#components/${this.name}`)
this.resolvedPath = `#components/${this.name}`
})
})
})
},
}
</script>
I've tried SO many different approaches but I'm fairly new to functional components and render functions (never got into React).
The roadblock : I can't seem to figure out how to run the chained functions that I call in my original mounted() function. I've tried running it from inside the render function with no success.
Big Question
How can I find and dynamically import the component I'm targeting before I pass that component to the createElement function (or within my single file <template functional><template/>)?
Thanks all you Vue-heads! ✌️
Update: I stumbled across this solution for using the h() render function and randomly loading a component, but I'm not sure how to make it work to accept the name prop...
Late to the party, but I was in a similar situation, where I had a component in charge of conditionally render one of 11 different child components:
<template>
<v-row>
<v-col>
<custom-title v-if="type === 'title'" :data="data" />
<custom-paragraph v-else-if="type === 'paragraph'" :data="data" />
<custom-text v-else-if="type === 'text'" :data="data" />
... 8 more times
</v-col>
</v-row>
</template>
<script>
export default {
name: 'ProjectDynamicFormFieldDetail',
components: {
CustomTitle: () => import('#/modules/path/to/CustomTitle'),
CustomParagraph: () => import('#/modules/path/to/CustomParagraph'),
CustomText: () => import('#/modules/path/to/CustomText'),
... 8 more times
},
props: {
type: {
type: String,
required: true,
},
data: {
type: Object,
default: null,
}
},
}
</script>
which of course is not ideal and pretty ugly.
The functional equivalent I came up with is the following
import Vue from 'vue'
export default {
functional: true,
props: { type: { type: String, required: true }, data: { type: Object, default: null } },
render(createElement, { props: { type, data } } ) {
// prop 'type' === ['Title', 'Paragraph', 'Text', etc]
const element = `Custom${type}`
// register the custom component globally
Vue.component(element, require(`#/modules/path/to/${element}`).default)
return createElement(element, { props: { data } })
}
}
Couple of things:
lazy imports don't seem to work inside Vue.component, hence require().default is the way to go
in this case the prop 'type' needs to be formatted, either in the parent component or right here
Im new to Vuejs and I get the error unknown custom element -
how do i register a custom element - b-alert. I think this element is from bootstrapVue.
<template>
<div>
<b-alert show>Default Alert</b-alert>
</div>
</template>
<script>
export default {
data () {
return {
dismissSecs: 10,
dismissCountDown: 0,
showDismissibleAlert: false
}
},
methods: {
countDownChanged (dismissCountDown) {
this.dismissCountDown = dismissCountDown
},
showAlert () {
this.dismissCountDown = this.dismissSecs
}
},
}
You will have to register the component as in Component registration
import { Alert } from 'bootstrap-vue/es/components';
components: { BAlert }
Since all html tags are turned into dashed by camelcase BAlert = '<b-alert>'
Alertenativly you can also use
components: { 'b-alert': BAlert }
I have a component that has a data property called rows, which are used to populate a dynamic table (I'm using Datatables).
I have a computed property called clientLeads that loads store data.
I have a watch function that watches the clientLeads computed property and updates the rows data property.
I would like to use the router-link component to create a Vue JS Router link to a profile page.
Here is the Datatables component (src: https://codepen.io/stwilson/pen/oBRePd):
<script>
import 'datatables.net-bs4'
import jQuery from 'jquery'
export default {
template: '<table class="table table-striped dt-responsive nowrap" style="width:100%"></table>',
props: {
headers: {
default () {
return {}
}
},
rows: {
default () {
return []
}
}
},
data () {
return {
dTHandle: null
}
},
watch: {
rows (val, oldVal) {
let vm = this
vm.dtHandle.clear()
vm.dtHandle.rows.add(vm.rows)
vm.dtHandle.draw()
}
},
mounted () {
let vm = this
vm.dtHandle = jQuery(this.$el).DataTable({
classes: {
sWrapper: 'dataTables_wrapper dt-bootstrap4'
},
columns: vm.headers,
data: vm.rows,
searching: false,
paging: false,
info: true,
responsive: {
details: {
type: 'column'
}
}
})
}
}
</script>
And here is my view component:
<template>
<div>
<data-tables :headers="headers" :rows="rows" />
</div>
</template>
<script>
import { mapGetters } from 'vuex'
export default {
data () {
return {
headers: [
{ title: 'Name' },
{ title: ' ' }
],
rows: []
}
},
computed: mapGetters({
clientLeads: 'clientLeads'
}),
watch: {
clientLeads (val, oldVal) {
let vm = this
let rows = []
val.forEach(function (item) {
let row = []
row.push(item.name)
row.push('<router-link to="/lead_details/' + item.id + '"><a><i class="fa fa-eye"></i></a></router-link>')
rows.push(row)
})
vm.rows = rows
}
},
created () {
// get leads
this.$store.dispatch('getClientLeads', {
status: this.status
})
}
}
</script>
However, the resulting html is:
<router-link to="/lead_details/123"><a><i class="fa fa-eye"></i></a></router-link>
...instead of:
<i class="fa fa-eye"></i>
I understand why but cannot figure out a workaround to resolve my issue.
The problem here is that datatables manipulates the DOM outside of Vue.js. As Vue doesn't handle the elements created by the lib, the router link component is not 'interpreted'.
If you want to keep going with this datables library maybe you can try to add a standard <a> tag with a # prefixed href :
<i class="fa fa-eye"></i>
As vue-router seems to prefix url with a # when you are in the default mode (hash-mode) : https://router.vuejs.org/guide/essentials/history-mode.html#example-server-configurations. Or you can just change the mode to history :
const router = new VueRouter({
mode: 'history',
routes: [...]
})
And get rid of the #.
Of course it supposes you have the matching route declared previously. It's a little tweaky as you will bypass vue-router like this and reload the page, but it works.
Working with vue, I strongly recommend, as much as possible, to prefered libraries that doesn't manipulate directly the DOM and let this task to Vue. Vue components give more options to be tweaked.
There are many components libraries that seems to do the job, for example :
https://vuetifyjs.com/en/components/data-tables
or http://element.eleme.io/#/en-US/component/table