Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

express - how to use require.js define a template like vue component and connect the data

In projects that use express + require.js and vue cdn, I try to use require.js to define a template similar to vue components

In index.js, I have a data list, I want to use v-for in the index.html display list item, but I cannot connect the data list in html

This is my code, is there any mistake?

index.js

define([
    'text!js/components/search/index.html',
    'jquery',
], function (template) {
    var $ = require('jquery');
    var Vue = require('vue');
    var ajax = require('js/ajax');
    return {
        name: 'search',
        template: require('text!js/components/search/index.html'),
        props: {

        },
        data: function () {
            return {
                // data list
                Options: [],
            };
        },
        mounted: function () {
            this.loadOptions();
        },
        methods: {
            //data list
            loadOptions() {
                ajax.get('/options/options').then(function (data) {
                    this.Options = data.Options;
                    console.log('this.Options Successful get data')
                });
            },
        },
    };
});
question from:https://stackoverflow.com/questions/65881554/how-to-use-require-js-define-a-template-like-vue-component-and-connect-the-data

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Maybe because of this?

I assume that console.log('this.Options Successful get data') works as expected. Try to change your loadOptions method as follows:

loadOptions() {
                ajax.get('/options/options').then(data => {
                    this.Options = data.Options;
                    console.log('this.Options Successful get data')
                });
            },

Arrow function should help to point this to your Vue component instance.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...