integrate vue js with asp.net core 2.1 - asp.net-core

Can someone help with explaining how I can use vue.js with asp.net core 2.1.
ideally i'd just like to hit f5 in visual studio 2017 and the website load.
i know ms previously had spa templates but they seem to have been abandoned. theres a starter template on github too but that is using .net core 2.0.

I am trying to use Vue.js with MVC Core 2.1 for a multi page application at the moment, and have found a working solution.
I see you mention SPA though, my answer will be for Multi page apps, but some of the ideas may apply.
My approach
I've created a main.js file in wwwroot/resources/js/ and a components subfolder within that directory - and also a layout folder in the components one.
In the main.js I create a Vue instance and attach it to #app root element in the HTML. In this js file, I also register most of the components globablly, unless they are only needed for specific components. My main.js looks something like this:
# wwwroot/resources/js/main.js
import Vue from 'vue';
import Axios from 'axios';
import BaseLayout from './components/layout/BaseLayout.vue';
window.axios = Axios;
Vue.component("base-layout", BaseLayout);
new Vue({
el: "#App"
});
In the _layout.cshtml file I have the following (relevant content):
# Views/Shared/_Layout.cshtml
<div id="App">
<base-layout>
#RenderBody()
</base-layout>
</div>
<script src="~/resources/js/bundle.js"></script>
If you are only interested in SPA - I would probably do something like:
# wwwroot/resources/js/main.js
import Vue from 'vue';
import App from './App.vue';
new Vue({
el: "#app",
render: (h) => h(App)
});
# wwwroot/resources/js/App.vue
<template>
<div id="app">
<-- Use other components here -->
</div>
</template>
<script>
export default {
name: "app",
data: function() {
return {
// What evs
}
}
}
</script>

VueJs (along with Angular and ReactJs) was available at one stage from the new project but was removed later on because of limited maintainers. However you can still create a VueJs app via command line. Run dotnet new -l to see the list of available dotnet templates.
if you don't see vuejs in the list, install the templates
dotnet new --install Microsoft.AspNetCore.SpaTemplates::*
Once you create the web app, and open it in the VS you should be able to run it like any other project, by hitting F5.
There are a few good resources online :
Asp.NETCore 2.1 Vue 2 Starter - by DevHelp.Online
Creating a VueJS (with TypeScript) SPA on ASP.Net Core 2.1
Second link mentions that vuejs template is still available via command line.
Also i found this series very informative, explains from setting up to deployment.
https://code-maze.com/net-core-series/

If you are building a multi-page application, you can take a look at this template as an example or starting point.
https://github.com/danijelh/aspnetcore-vue-typescript-template
If you're building an SPA, consider splitting your API from your UI and use Vue CLI 3 for the UI part.

Related

How to prevent caching variable from js file in NuxtJS

Please help me to find right solution. I have NuxtJS project, there is a file structure as follows:
.nuxt
assets
components
dist
layouts
middleware
mixins
node_modules
pages
plugins
static
store
utils
versions
There is the folder versions where I store files for app versions:
app-version.js
update-version.js
versions-log.txt
I export version value from app-version.js:
exports.APP_VERSION = '1.1.0.0';
And then import it in Vue component to show it in UI
<span>{{ version }}</span>
...
<script>
...
import {APP_VERSION} from '~/versions/app-version';
...
computed: {
version() {
return APP_VERSION;
}
}
</script>
The problem is that sometimes app version is incorrect in UI, I think that it is saved in cache. After clearing cache of browser it is OK.
I don't understand why there is the problem. I thought that Vue must handle with it and prevent caching data. Maybe I need hashing js file with version variable, but I don't know how to do it.
Can anyone help me? I would be greatefull for any advise.

Vue 3 + Module Federation

I am trying to build a simple micro-frontend example using Vue3 and Module Federation but I have a collision problem.
I have the code here:
https://github.com/ghalex/mf-example
There are only two mf:
container (Vue3)
auth (Vue3)
The problem I have is that when I import the App.vue component:
// index.js - in container
import App from './components/App.vue'
// indexAuth.js - in auth
import App from './components/App.vue'
in the auth module and load the module in container the App.vue component is overwritten.
Is there any webpack settings am I missing to make sure each component is loaded separately ?
I found the problem, it is a webpack-dev-server v4 bug. I downgraded to v3 and everything works fine.

Vuetify errors when loading a Vue-generated web component into a host/parent that is also a Vue app

I have a Vue Cli 3 generated web component/custom element that uses Vuetify. I have a parent/host Vue App that is also using Vuetify. When the custom element loads into the host app, I get a bunch of Type errors as shown in the screenshot. e.g. Type Error: Cannot read property theme of undefined.
This appears to be a Vuetify issue. What is the correct architecture for using Vuetify within a Vue CLi custom element and in its host application?
Currently, I have Vue.js script tag in parent, for use by the custom element. And Vue is imported into the Vue app, as normal. Script tag is there only because the custom element requires it. I have Vuetify included in both as well. The web component is using the vuetify plugin. And so is the host.
The web component runs perfectly fine on its own, and so does the host app. So, apparently there are conflicts.
Thanks!
Donnie
Resolved this myself. In short, you cannot use the Vuetify plugin and the al-a-cart. It only works if you include the full Vue and Vuetify inside the web component. Not optimal, but at least it is a workaround for this Vuetify bug.
import Vue from "vue";
import Vuetify from "vuetify";
Vue.use(Vuetify);
// import { VApp,VToolbar,VToolbarTitle,VSpacer,VContainer,VLayout,VFlex,VCard,VCardTitle,VCardActions,VBtn,VDivider } from 'vuetify/lib';
export default {
/* components:{
VApp,VToolbar,VToolbarTitle,VSpacer,VContainer,VLayout,VFlex,VCard,VCardTitle,VCardActions,VBtn,VDivider
} */
}

Start Quasat/Vue app inside a plugin Promise

For an app I need to load dynamic configurations (read from external service) before I can bootstrap my Quasar app.
In other Vue apps I use a require.ensure or a Promise in main.js and load the app after everything else is ready, but since Quasar added the concept of App Plugins this won't work anymore as there is no Promises or Chaining available, resulting in race conditions as the App needs configurations that are not available yet.
I looked at their internals and they seem to load plugins in a forEach block from an auto-generated file, not ready for this scenario.
Here's an excerp from the usual solution in main.js (using require.ensure as the file auto-generated on the same server by another service)
require.ensure(['./config.js'], (require) => {
const config = require('.config.js');
// ... use values in config to bootstrap axios, apollo and such
// And finally start the Vue app
new Vue({ ... })
})
Any ideas on how to do this in a Quasar or Webpack way ?
The only alternative I can think of now is to fork&patch Quasar, but maybe I'm missing something and there is another way.

My app doesn't compile after Vue CLI 2 to 3 update

I get the following error since I switched from Vue CLI version 2 to 3:
You are using the runtime-only build of Vue where the template
compiler is not available. Either pre-compile the templates into
render functions, or use the compiler-included build.
Here's how I instantiate Vue:
new Vue({
el: '#app',
store,
router,
components: {
UserStatus
},
data: {
isLoading: true
}
})
This worked with version 2, why not on version 3?
This answer proposes to import Vue's template compiler via import Vue from 'vue/dist/vue.esm.js';, however this creates issues with Vuetify, and I still don't understand why there's any need to import the template compiler if version 2 didn't need to.
Just in case here's the content of my index.html. Also here's my app's entire codebase.
As a reminder here's the out-of-the-box way of instantiating the main Vue instance, which is inadequate for me because it overrides whatever I manually wrote inside the <div id="app"> element in my index.html, and also involves an App.vue component which I actually don't have or want to have:
new Vue({
router,
store,
render: h => h(App)
}).$mount('#app')
The project seems to be missing quite a few dependencies related to vue cli 3. I forked your repo and brought package.json up to speed with a fresh install as well as aliased the runtime + compiler build of Vue.
Everything compiled and the console was clear of any errors related to compiling templates. The page never loaded, however, which I suspect is due to Firebase credentials missing.
You can find my updated fork here on GitHub. Hope it helps!
I also faced this issue and I got it working by creating a vue.config.js file at my project's root directory and wrote the following code in it.
module.exports = {
runtimeCompiler: true,
};