Coder Social home page Coder Social logo

Comments (11)

arcadeJHS avatar arcadeJHS commented on June 20, 2024 3

@Jvogel-Amplify ... which you can for sure read (together with code examples) on my github: https://github.com/arcadeJHS/AngularVueIntegration.

Hope it helps!

from ngvue.

jaredmcateer avatar jaredmcateer commented on June 20, 2024

You can use the slot element in the vue template, however, the implementation is buggy and it seems like they are considering deprecating it.

I am also in the same boat as you, we're trying to migrate our angular app to vue and having the ability to have angular directives/components within a vue component is necessary.

I've found that you cannot have any whitespace between your vue component tags and the slot content. Your angular code cannot have any translcudes and there are some other buggy behaviours that I'm trying to work around atm.

E.g., Your example would have to be written as:

<old-angular-app>
    <ng-vue-component><old-existing-angular-component /></ng-vue-component>
</old-angular-app>

from ngvue.

arcadeJHS avatar arcadeJHS commented on June 20, 2024

Thank you for the suggestion @jaredmcateer!
I've found the buggy implementation mentioned in the issue #57.
At this point I am not sure if following this way (trying slots) it's a "good" choice... but still, as you, I have the necessity to keep angular code inside vue while migrating.

from ngvue.

nicolaspayot avatar nicolaspayot commented on June 20, 2024

I'm sorry but as you said, ngVue was not designed to allow AngularJS components to be rendered inside VueJS components. We've tried to add slots but it doesn't work in multiple situations: #66, due to the difference of rendering mechanism between AngularJS and Vue components.

However, you could compile an AngularJS component template within mounted() hook for example, inside your Vue component, and add the compiled element to the current element (this.$el):

mounted() {
    const template = `<custom-ng-tag foo="$ctrl.foo"></custom-ng-tag>`;

    const bindings = { foo: this.foo };

    const scope = angular.extend($rootScope.$new(), { $ctrl: bindings });
    const element = $compile(template)(scope);

    this.$el.append(element[0]);
}

This should kinda work but this is very experimental, imho. I would advise not to use it in production 😅

from ngvue.

arcadeJHS avatar arcadeJHS commented on June 20, 2024

Thank you @nicolaspayot!
I will experiment a little bit... as I said, I would prefer to preserve part on the existing angular code in a new Vue wrapper, and my main concern is how to handle the $scope in the existing angular code (which is quite pervasive and entangled into the application... ugly as it was...)!

from ngvue.

arcadeJHS avatar arcadeJHS commented on June 20, 2024

@nicolaspayot: I am sorry, but I have a couple of (maybe stupid) question!

  1. It seems to me that using an angular component through slot seems to work in a "normal" template, but does not work when loading a component via vue-router. Is it right?

  2. In your example here above, how are you using $rootScope (and $compile)? It seems you are using it as a global object, but that should not be available there. Anyway, so far I got it working with a slightly different version:

mounted() {
    const el = angular.element(this.$el);
    el.injector().invoke(['$compile', '$rootScope', function($compile, $rootScope) {
        const scope = angular.extend($rootScope.$new(), {});
        $compile(el)(scope);
    }]);
}

Well... working... kind of... :-P

from ngvue.

arcadeJHS avatar arcadeJHS commented on June 20, 2024

I've come to this "AngularComponent.vue" component:

<template>
	<div></div>
</template>

<script>
export default {
	name: "AngularComponent",
	props: ['component'],
	mounted () {
		const el = angular.element(this.$el);
		const componentTemplate = this.component.template;
		const componentCtrl = this.component.$ctrl;

		el.injector().invoke(['$compile', '$rootScope', function($compile, $rootScope) {
			const scope = angular.extend($rootScope.$new(), {$ctrl: componentCtrl});
			el.append($compile(componentTemplate)(scope));
		}]);
	}
};
</script>

Inside a Vue component, to include an Angular component, I use it in this way:

<template>
	<div>
		<angular-component :component="angularComponent"></angular-component>
		<div>Vue template</div>
	</div>
</template>

<script>
import AngularComponent from './components/AngularComponent';

export default {
	name: "AngularInsideVue",
	components: { AngularComponent },
	data() {
		return {
			angularComponent: {
				template: '<example-component some-param="$ctrl.someParam"></example-component>',
				$ctrl: { someParam: 'Hello from Angular!' }
			}
		};
	}
};
</script>

Still I need to test it with more complex angular components, bindings, and updates, but it seems to work.

from ngvue.

Jvogel-Amplify avatar Jvogel-Amplify commented on June 20, 2024

@arcadeJHS Did you ever get this working with bindings, updates, etc...? I have a similar use case as you

from ngvue.

arcadeJHS avatar arcadeJHS commented on June 20, 2024

@Jvogel-Amplify Well, I guess a sort of! :)
If you are interested in more details, I wrote on Medium a detailed review of that titled "Migrating an Angular 1.x app to Vue 2.x".

from ngvue.

Jvogel-Amplify avatar Jvogel-Amplify commented on June 20, 2024

This is a great guide thank you for documenting all of this! I am considering migrating an angular 1.x app to Vue so this will be a big help.

from ngvue.

arcadeJHS avatar arcadeJHS commented on June 20, 2024

@Jvogel-Amplify: no problem, thank you for reading! :-)
That, for sure, is my point of view... but I hope it helps.

from ngvue.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.