How do you include vuetify inside web component - vue.js

I'm building a web component using the following command :
vue-cli-service build --target wc --name my-element 'src/components/mycomponent.vue'
I would like to use Vuetify inside of this component. How do I add it to mycomponent.vue so that it is scoped inside the component's shadow root?
This component will be dynamically loaded into apps built with other frameworks/styles. I want the web component to be able to use, for example, v-btn, v-layout, etc. within it.
Thank you,
Donnie

For vuetify 2.x, it requires initialization on Vue instance as follows.
// plugins/vuetify.js
import Vue from 'vue'
import Vuetify from 'vuetify/lib'
Vue.use(Vuetify);
const opts = {};
export default new Vuetify(opts);
// main.js
import Vue from 'vue';
import App from './app.vue';
import vuetify from './plugins/vuetify';
new Vue({
vuetify,
render: h => h(App),
}).$mount('#app');
You need to move such initialization into your web component instead.
<template>
...
</template>
<script>
import { VBtn, VLayout } from 'vuetify/lib'
import vuetify from '../plugins/vuetify';
export default {
name: 'MyWebComponent',
vuetify,
components: {
VBtn,
VLayout
},
...
}
</script>
<style>
...
</style>

From v1.3, you can import individual components, A La Carte...
<template>
<!-- whatever -->
</template>
<script>
import { VBtn, VLayout } from 'vuetify/lib'
export default {
name: 'MyElement',
components {
VBtn,
VLayout
},
// etc
}
</script>
See https://vuetifyjs.com/en/framework/a-la-carte#importing-components

Related

Add only Vuetify data table component to existing Vuejs Project

I need to add Vuetify Data Table to my existing Vuejs project. So when we add vue add vuetify its installed globally. Please help me for that how doing it
Thanks
You can import the dataTable component alone.
You can do this in a vuetify.js file:
// src/plugins/vuetify.js
import Vue from 'vue'
import Vuetify, {
VDataTable,
} from 'vuetify/lib'
Vue.use(Vuetify, {
components: {
VDataTable,
},
})
const opts = {}
export default new Vuetify(opts)
and in your main.js import this file: import vuetify from #/src/plugins/vuetify.js
or you can import it in the component you need it:
<!-- Vue Component -->
<template>
<v-card>
<v-card-title>...</v-card-title>
<v-card-text>...</v-card-text>
</v-card>
</template>
<script>
import { VDataTable } from 'vuetify/lib'
export default {
components: {
VDataTable,
}
}
</script>
you can read more about this in vuetify docs

How can I set up moment.js in the vuetify?

I using vuetify : https://vuetifyjs.com/en/
I want to use moment.js. So I read this reference : https://www.npmjs.com/package/vue-moment
I had run npm install vue-moment
I'm still confused to put this script Vue.use(require('vue-moment'));
In the vuetify, there exist two file : main.js and index.js
main.js like this :
import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store/index'
import './registerServiceWorker'
import vuetify from './plugins/vuetify'
Vue.config.productionTip = false
new Vue({
router,
store,
vuetify,
render: h => h(App)
}).$mount('#app')
index.js like this :
import Vue from 'vue';
import Vuex from 'vuex';
import dataStore from './modules/data-store';
import createLogger from "vuex/dist/logger";
Vue.use(Vuex);
const debug = process.env.VUE_APP_DEBUG !== "production";
export default new Vuex.Store({
modules: {
dataStore
},
strict: debug,
plugins: debug ? [createLogger()] : []
});
where do i put Vue.use(require('vue-moment'));?
I try to put it in the main.js, but if i call my vue component, there exist error : ReferenceError: moment is not defined
My vue component like this :
<template>
...
</template>
<script>
export default {
mounted() {
let a = moment("2012-02", "YYYY-MM").daysInMonth();
console.log(a)
}
};
</script>
I found this at the bottom of the vue-moment npm page
vue-moment attaches the momentjs instance to your Vue app as
this.$moment.
This allows you to call the static methods momentjs provides.
So you should be able to use your original configuration of vue-moment and do this in your mounted() method
mounted() {
let a = this.$moment("2012-02", "YYYY-MM").daysInMonth();
console.log(a)
}
notice this.$moment
And for the set up of vue-moment you should place this in your main.js file
main.js
Vue.use(require('vue-moment'))
=========================================================================
GLOBAL
If you want to use moment with Vue globally you can create an Instance Proprety
main.js
import moment from 'moment'
Vue.prototype.moment = moment
In your component you then call this.moment in your methods or computed properties. In your mounted section it would look like this
mounted() {
let a = this.moment("2012-02", "YYYY-MM").daysInMonth();
console.log(a)
}
COMPONENT
If you just want to use moment in a component you can include directly like this
<script>
import moment from 'moment'
export default {
mounted(){
let a = moment("2012-02", "YYYY-MM").daysInMonth();
console.log(a)
}
}
</script>

Unknown custom element: <v-app> - did you register the component correctly? For recursive components

Hey I have a problem with importing vuetify into my project...
What am I doing wrong?
[Vue warn]: Unknown custom element: - did you register the
component correctly? For recursive components, make sure to provide
the "name" option.
app.js:
import Vue from "vue";
import Vuetify from "./plugins/vuetify";
import store from "~/store";
import router from "~/router";
import App from "~/components/App";
Vue.config.productionTip = false;
/* eslint-disable no-new */
new Vue({
store,
router,
Vuetify,
...App
});
App.vue:
<template>
<v-app>
<loading ref="loading" />
<router-view />
</v-app>
</template>
<script>
import Loading from "./Loading";
export default {
el: "#app",
components: {
Loading
}
};
</script>
<style>
</style>
plugins/vuetify.js:
import Vue from "vue";
import Vuetify from "vuetify/lib";
Vue.use(Vuetify);
export default new Vuetify({
icons: {
iconfont: "md" // 'mdi' || 'mdiSvg' || 'md' || 'fa' || 'fa4'
},
theme: {
dark: false
},
themes: {
light: {
primary: "#4682b4",
secondary: "#b0bec5",
accent: "#8c9eff",
error: "#b71c1c"
}
}
});
Had the same issue which has bugged my head for like an hour now, how this worked I don't know either: but this is what I changed when importing vuetify in vuetify.js
changed:
import Vuetify from 'vuetify/lib'
replaced it with:
import Vuetify from 'vuetify'
Note: this could be a laravel issue only because the official vuetify documentation has the first form.
got that project created with vue-cli v3? You either need to register components yourself or have a vuetify loader added, that parses your components and generates that list itself. the respective docu you can find here https://vuetifyjs.com/en/customization/a-la-carte#vuetify-loader
To add Vuetify to existing project you should follow the below procedure
In your project folder run,
npm install vuetify --save
In your app.js
import Vuetify from 'vuetify/lib'
// To add vuetify css file
import 'vuetify/dist/vuetify.min.css'
Vue.use(Vuetify)
export default new Vuetify({ ... })
To finish, you need to add a v-app component that wrap your entire application in order to make Vuetify works.
<v-app id="app">
<router-view/>
</v-app>
Change
new Vue({
store,
router,
Vuetify,
...App
});
To
store,
router,
vuetify: Vuetify,
...App
});
Step 1: Install Vuetify in your Project using "vue add vuetify" command
Step 2: In main.js write following code
import vuetify from './plugins/vuetify'; //plugins folder installed when you add vuetify
new Vue({
vuetify,
render: h => h(App)
}).$mount('#app');
Step 3: Restart your Project because whenever you changed in main.js file then you need to restart your Project.
Docs at https://vuetifyjs.com/en/getting-started/unit-testing/#testing-efficiency suggest to create an instance pass it to mount.
beforeEach(() => {
vuetify = new Vuetify()
})
const wrapper = mount(CustomCard, {
localVue,
vuetify
})

How do I add Vuetify 2.0 to an existing project?

I've recently upgraded from Vuetify 1.5 to Vuetify 2.0 and I'm having trouble getting it to work. I feel like I'm missing something.
I downloaded the newest Vuetify package and the #mdi/font package as well. I've followed the instructions in the docs, as far as I can tell: I've added the plugins folder with the vuetify.js file, and as far as I can tell, I've instantiated Vuetify into my Main.js file properly, but none of the stylings appear in my app. I've also tried adding a element tag to my project in various places (my App.vue file and various other page files), but all that seems to do is break things even more; I either get an element to appear on the DOM with no stylings, or the DOM comes up completely white.
Here is my vuetify.js file:
import Vue from "vue";
import Vuetify from "vuetify";
import "#mdi/font/css/materialdesignicons.css";
Vue.use(Vuetify);
export default new Vuetify({
icons: {
iconfont: "mdi"
}
});
Here is my main.js file:
import Vue from "vue";
import App from "./App.vue";
import router from "./Router";
import Vuetify from "#/plugins/vuetify";
import 'vuetify/dist/vuetify.min.css'
Vue.config.productionTip = false;
new Vue({
router,
Vuetify,
render: h => h(App)
}).$mount("#app");
Here is my App.vue file:
<template>
<div id="app">
<Header />
<router-view></router-view>
<Footer />
</div>
</template>
<script>
import Header from "./components/Layout/Header";
import Home from "./components/Home";
import InstructorProfile from "./components/InstructorProfile";
import ClassRoster from "./components/ClassRoster";
import Footer from "./components/Layout/Footer";
export default {
name: "app",
components: {
Header,
Home,
InstructorProfile,
ClassRoster,
Footer
},
data() {
return {};
}
};
</script>
As I mentioned before, I have tried adding elements to this file, both like this:
<v-app>
<div id="app">...</div>
</v-app>
And like this:
<div id="app">
<v-app>...</v-app>
</div>
But neither seemed to work better than the other.
I'd like to know if there's something I've left out or I've done wrong.
Any help is much appreciated. Thank you in advance.
try with this:
In vuetify.js file:
import Vue from "vue";
import Vuetify from "vuetify";
import "vuetify/dist/vuetify.min.css"; // Add this line
Vue.use(Vuetify);
const opts = {
theme: {
dark: false
},
options: {
customProperties: true
},
icons: {
iconfont: "mdi"
}
};
export default new Vuetify(opts);
In main.js file:
import Vue from "vue";
import App from "./App.vue";
import router from "#/router";
import vuetify from "#/plugins/vuetify";
Vue.config.productionTip = false;
new Vue({
vuetify,
router,
render: h => h(App)
}).$mount("#app");
I do it this way (vue 3.9, vuetify 2.0)
In main.js
import vuetify from './plugins/vuetify'
...
new Vue({
...
vuetify,
render: h => h(App)
}).$mount('#app')
In plugins/vuetify.js
import Vue from "vue"
import Vuetify from "vuetify/lib"
Vue.use(Vuetify)
export default new Vuetify({
icons: {
iconfont: 'md', // 'mdi' || 'mdiSvg' || 'md' || 'fa' || 'fa4'
},
theme: {
dark: false,
},
themes: {
light: {
primary: "#4682b4",
secondary: "#b0bec5",
accent: "#8c9eff",
error: "#b71c1c",
},
},
})
in App.vue
<template>
<v-app>
...
</v-app>
</template>
I fixed with
npm install vuetify-loader vue-cli-plugin-vuetify -D
https://vuetifyjs.com/en/getting-started/frequently-asked-questions/#questions
I just had to run: vue add vuetify.
From the docs for Vuetify 3
Follow these steps if for example you are adding Vuetify to an existing project, or simply do not want to use a scaffolding tool.
yarn add vuetify#^3.0.0
In the file where you create the Vue application, add the following code
import { createApp } from 'vue'
import App from './App.vue'
// Vuetify
import 'vuetify/styles'
import { createVuetify } from 'vuetify'
import * as components from 'vuetify/components'
import * as directives from 'vuetify/directives'
const vuetify = createVuetify({
components,
directives,
})
createApp(App).use(vuetify).mount('#app')
See the docs for more info.

How to use global component from a dependancy in Vue2

I am using vue-form-generator and trying to get it to render a simple form without success. I am going to re-use this generator in dozens of files as my application is form heavy and would like to make this global and I'll just manage everything in components.
main.js file
import Vue from "vue";
import App from "./App.vue";
import router from "./router";
import store from "./store";
import BootstrapVue from 'bootstrap-vue';
import "./registerServiceWorker";
import VueFormGenerator from "vue-form-generator";
Vue.use("VueFormGenerator", VueFormGenerator);
Vue.config.productionTip = false;
Vue.use(BootstrapVue);
new Vue({
router,
store,
render: h => h(App)
}).$mount("#app");
I have a larger template that uses this template for most of the page, this is the template file where I am trying to use VueFormGenerator
NewTrade.vue
<template>
<div>
<p class="introText">Please try to stick to your strategy and use the Manual mode as little as possible if at all! </p>
<form action="#">
<VueFormGenerator
:schema="schema"
:model="model"
/>
</form>
</div>
</template>
<script>
export default {
data() {
return {
model: {
name: "David Johnson"
},
schema: {
fields: [
{
name: "text"
}
]
}
}
}
}
</script>
This is the error I get in Chrome.
[Vue warn]: Unknown custom element: <VueFormGenerator> - did you register
the component correctly? For recursive components, make sure to provide the
"name" option.
found in
---> <NewTrade> at src/components/NewTrade.vue
<Trading> at src/components/Trading.vue
<App> at src/App.vue
<Root>
just use component instead of use
import Vue from "vue";
import App from "./App.vue";
import router from "./router";
import store from "./store";
import BootstrapVue from 'bootstrap-vue';
import "./registerServiceWorker";
import VueFormGenerator from "vue-form-generator";
Vue.component("VueFormGenerator ", VueFormGenerator); // <-- component instead of use
Vue.config.productionTip = false;
Vue.use(BootstrapVue);
new Vue({
router,
store,
render: h => h(App)
}).$mount("#app");
Usually you'd use vue-form-generator instead of VueFormGenerator as the component name here and in template, as that's the convention, but you only need that when you compile run-time.
here is a working example: https://codesandbox.io/s/o77lmqq9v6