44import java .util .Map ;
55import java .util .Map .Entry ;
66
7+ import io .github .jeemv .springboot .vuejs .configuration .VueVersion ;
8+ import io .github .jeemv .springboot .vuejs .configuration .versions .Vue2Version ;
9+ import io .github .jeemv .springboot .vuejs .configuration .versions .Vue3Version ;
710import jakarta .annotation .PostConstruct ;
811import org .slf4j .Logger ;
912import org .slf4j .LoggerFactory ;
@@ -33,13 +36,21 @@ public class VueJS extends AbstractVueJS {
3336 protected String [] delimiters ;
3437 protected boolean useAxios ;
3538 protected boolean vuetify ;
39+
40+ protected String vueVersion ;
3641 protected Map <String , VueComponent > globalComponents ;
3742 protected Map <String , AbstractVueComposition > globalElements ;
3843
3944 @ Autowired (required = false )
4045 protected VueJSAutoConfiguration vueJSAutoConfiguration ;
4146 private final Logger logger ;
4247
48+ protected VueVersion getVueVersion () {
49+ if (isVue2 ()) {
50+ return new Vue2Version (useAxios );
51+ }
52+ return new Vue3Version (useAxios );
53+ }
4354 @ PostConstruct
4455 public void init () {
4556 if (null == vueJSAutoConfiguration ) {
@@ -54,6 +65,7 @@ public void init() {
5465 vuetify = true ;
5566 }
5667 el = vueJSProperties .getEl ();
68+ vueVersion = vueJSProperties .getVueVersion ();
5769 }
5870 }
5971
@@ -65,6 +77,7 @@ public VueJS(String element) {
6577 this .el = element ;
6678 globalComponents = new HashMap <>();
6779 globalElements = new HashMap <>();
80+ vueVersion =VueVersion .VUE_3 ;
6881 logger = LoggerFactory .getLogger (VueJS .class );
6982 }
7083
@@ -92,18 +105,17 @@ public void setDelimiters(String start, String end) {
92105 */
93106 @ Override
94107 public String getScript () {
108+ VueVersion vueVersion = getVueVersion ();
95109 StringBuilder script = new StringBuilder ();
96- if (useAxios ) {
97- script = new StringBuilder ("Vue.prototype.$http = axios;\n " );
98- }
110+
99111 try {
100112 for (Entry <String , VueComponent > entry : globalComponents .entrySet ()) {
101113 script .append (entry .getValue ());
102114 }
103115 for (Entry <String , AbstractVueComposition > entry : globalElements .entrySet ()) {
104116 script .append (entry .getValue ().getScript ());
105117 }
106- script .append ("new Vue(" ). append (JsUtils .objectToJSON (this )). append ( ");" );
118+ script .append (vueVersion . generateVueJSInstance (JsUtils .objectToJSON (this ), el ) );
107119 return "<script>" + script + "</script>" ;
108120 } catch (JsonProcessingException e ) {
109121 e .printStackTrace ();
@@ -175,4 +187,8 @@ public boolean isVuetify() {
175187 return vuetify ;
176188 }
177189
190+ public boolean isVue2 () {
191+ return vueVersion .startsWith ("2" );
192+ }
193+
178194}
0 commit comments