无法在vue js中使用外部js文件,尝试在挂载中追加到头部,但无法正常工作

时间:2019-03-29 17:49:35

标签: vue.js

我正在尝试在vue cli中使用HTML管理模板。该模板中有一些js文件。当我尝试使用vue js时,刷新后它们可以工作,但是当我将一个组件转移到另一个组件时,它们却无法工作。

例如:jquery.min.js等。

mounted () {
let jqueryscript = document.createElement('script')
jqueryscript.setAttribute('src', '/js/jquery-2.1.1.min.js')
document.body.appendChild(jqueryscript) }

1 个答案:

答案 0 :(得分:0)

This应该可以解决问题。请记住,如果您在package main import ( "fmt" "log" "net/http" "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/service/ec2" "github.com/gorilla/mux" ) func createInstance(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") sess, err := session.NewSession(&aws.Config{ Region: aws.String("us-west-1")}, ) // Create EC2 service client svc := ec2.New(sess) // Specify the details of the instance that you want to create. runResult, err := svc.RunInstances(&ec2.RunInstancesInput{ // An Amazon Linux AMI ID for t2.micro instances in the us-west-2 region ImageId: aws.String("ami-xxxxxx"), InstanceType: aws.String("t2.micro"), MinCount: aws.Int64(1), MaxCount: aws.Int64(1), }) if err != nil { fmt.Println("Could not create instance", err) return } fmt.Println("Created instance", *runResult.Instances[0].InstanceId) // Add tags to the created instance _, errtag := svc.CreateTags(&ec2.CreateTagsInput{ Resources: []*string{runResult.Instances[0].InstanceId}, Tags: []*ec2.Tag{ { Key: aws.String("Name"), Value: aws.String("test"), }, }, }) if errtag != nil { log.Println("Could not create tags for instance", runResult.Instances[0].InstanceId, errtag) return } } func main() { r := mux.NewRouter() //init the router fmt.Println("Successfully tagged instance") r.HandleFunc("/api/instance", createInstance).Methods("GET") log.Fatal(http.ListenAndServe(":8500", r)) } 中提供的URL不存在或无法访问(至少在CodePen上),则此操作将失败。

完整链接: https://codepen.io/oze4/pen/pBzexp?editors=1010


HTML:

src=%url%

JS / Vue:

<body>
  <div id="app">
    <h1>Wait for 2 seconds!</h1>
    <br />
    <h1>You will receive an alert.</h1>
    <br />
    <h1 style="color: red;">Click "Run" to run again!</h1>
  </div>
</body>

成功!

enter image description here