1

I have added lightning component to salesforce1 application. But is there any way to add a lightning application to S1 app? Because lightning component can't add script files. Therefore I can't load scripts files to the lightning component.Is there any solution for this issue?

1

You can add script and css file in your component controller using java script

1) add following line into your yourcomp.cmp file

<aura:handler name="init" value="{!this}" action="{!c.doInit}" />

2) Add doInit function into yourcompController.js

({
    doInit : function(component, event, helper) {
       helper.assets();
    }
})

3) Add following code into yourcompHelper.js


({
  assets : function() {
        this.loadCSS('/resource/assets/css/mycss.css', function() {
            console.log('Loaded mycss.css');
        });
        this.loadJS('/resource/assets/js/jquery.js', function() {
            console.log('Loaded jquery.js ');
        });
  },
    loadJS : function(source, callback) {
        console.log('Going to load '+source+ ' java script file')
        var loadScript = document.createElement('script');
        loadScript.setAttribute('src', source);
        loadScript.onload = callback;
        document.body.appendChild(loadScript);
    },
    loadCSS : function(source, callback) {
        console.log('Going to load '+source+ ' style file')
        var fileref = document.createElement('link');
        fileref.setAttribute("rel", "stylesheet");
        fileref.setAttribute("type", "text/css");
        fileref.setAttribute("href", source);
        fileref.onload = callback;
        document.head.appendChild(fileref);
    }
})

Note: Your static path should be correct.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.